最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RegExp to get text in a key value pair separated by a colon - Stack Overflow

programmeradmin2浏览0评论

I have my Regexp example here:

For the following string:

key_1: some text, maybe a ma, ending in a semicolon; key_2: text with possibly no ending semicolon, but could be

I would like to do something like the following:

var regEx_attrVal = /[\w :].*?(?=;|$)/g;
var results = attrs.match(regEx_attrVal);
      for(i=0;i<results.length;++i) {
        var split = results[i].split(':');

        preColon = split[0].trim();
        postColon = split[1].trim();

        //log preColon and postColon to console

      }

the end result should be something like:

//results[0]
preColon: key_1
postColon: some text, maybe a ma, ending in a semicolon

//results[1]
preColon: key_2
postColon: text with possibly no ending semicolon, but could be

My regexp is definitely wrong, hoping the SO munity can help!

Thanks!

I have my Regexp example here: https://regex101./r/kE9mZ7/1

For the following string:

key_1: some text, maybe a ma, ending in a semicolon; key_2: text with possibly no ending semicolon, but could be

I would like to do something like the following:

var regEx_attrVal = /[\w :].*?(?=;|$)/g;
var results = attrs.match(regEx_attrVal);
      for(i=0;i<results.length;++i) {
        var split = results[i].split(':');

        preColon = split[0].trim();
        postColon = split[1].trim();

        //log preColon and postColon to console

      }

the end result should be something like:

//results[0]
preColon: key_1
postColon: some text, maybe a ma, ending in a semicolon

//results[1]
preColon: key_2
postColon: text with possibly no ending semicolon, but could be

My regexp is definitely wrong, hoping the SO munity can help!

Thanks!

Share Improve this question asked May 11, 2016 at 9:02 Ron IRon I 4,2508 gold badges38 silver badges67 bronze badges 4
  • 2 Try (\w+):\s*([^;]*), there will be no need splitting. Just access Groups 1 and 2. – Wiktor Stribiżew Commented May 11, 2016 at 9:04
  • @WiktorStribiżew nice! you saved me time and frustration - thank you! – Ron I Commented May 11, 2016 at 9:10
  • @WiktorStribiżew - yes please, as that is the answer to the question. I wish I could also mark gurvinder372's answer, because it does solve the problem as well, although yours is directly related to the post of how to do it in Regex. Any idea on the performance difference between regex and split approaches when dealing with something like 1000 of these queries? – Ron I Commented May 11, 2016 at 9:19
  • As for the performance, the only way to tell is to test. – Wiktor Stribiżew Commented May 11, 2016 at 9:21
Add a ment  | 

3 Answers 3

Reset to default 3

Here is a regex way to extract those values:

/(\w+):\s*([^;]*)/gi

or (as identifiers should begin with _ or a letter):

/([_a-z]\w*):\s*([^;]*)/gi

Here is a regex demo

var re = /([_a-z]\w*):\s*([^;]*)/gi; 
var str = 'key_1: some text, maybe a ma, ending in a semicolon; key_2: text with no ending semicolon';
while ((m = re.exec(str)) !== null) {
    document.body.innerHTML += m[1] + ": " + m[2] + "<br/>";
}

Pattern details:

  • ([_a-z]\w*) - Group 1 matching an identifier starting with _ or a letter and followed with 0+ alphanumeric/underscore symbols
  • : - a colon
  • \s* - 0+ whitespaces
  • ([^;]*) - 0+ characters other than ;. The use of a negated character class eliminates the need of using lazy dot matching with (?:$|;) group after it. NOTE that * quantifier makes the value optional. If it is required, use +.

You need to add a g modifier, DEMO

If regex is not mandatory then try

var input = "key_1: some text, maybe a ma, ending in a semicolon; key_2: text with no ending semicolon";
var keyValues = input.split(";");
keyValues.forEach( function(val){
  var keyValue = val.split( ":" );
  alert( "precolon " + keyValue[0] );
  alert( "postcolon " + keyValue[1] );
});

This regex should to the trick for you:

/(\w+):([^;]*)/g

Example here

发布评论

评论列表(0)

  1. 暂无评论