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

javascript - Regex group matches with space - Stack Overflow

programmeradmin2浏览0评论

I have simple problem with regex, but I haven't idea for resolve them. I have string (in grey this is a label):

cccc:ddddd bbbb:fgggg aaa aa:ddd ddd cccc:ggggggg

and regex

/(aaa aa|bbbb|cccc)+:([\sa-zA-Z]*)(?:$|\s)/ig

After parsing string 'label' aaa aa is ignoring, because have space and is taken to second match. I want to do first match labels (with white space or not), colon and anything (with spaces) after insert into second match to next 'label' or end line.

Any suggestions?

I have simple problem with regex, but I haven't idea for resolve them. I have string (in grey this is a label):

cccc:ddddd bbbb:fgggg aaa aa:ddd ddd cccc:ggggggg

and regex

/(aaa aa|bbbb|cccc)+:([\sa-zA-Z]*)(?:$|\s)/ig

https://regex101./r/mR3vK5/1

After parsing string 'label' aaa aa is ignoring, because have space and is taken to second match. I want to do first match labels (with white space or not), colon and anything (with spaces) after insert into second match to next 'label' or end line.

Any suggestions?

Share Improve this question edited Jul 9, 2016 at 6:40 Steve 1,6252 gold badges21 silver badges33 bronze badges asked Jul 8, 2016 at 21:44 PeterPeter 50110 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

If you know all the keys you may use them inside the positive lookahead and match the values with lazy dot:

/(aaa aa|bbbb|cccc):(.*?)(?=$|\s+(?:aaa aa|bbbb|cccc))/gi

See the JS demo:

var block = "aaa aa|bbbb|cccc";
var rx = RegExp("(" + block + "):(.*?)(?=$|\\s+(?:" + block + "))", "ig");
var s = "cccc:ddddd bbbb:fgggg aaa aa:ddd ddd cccc:ggggggg";
while ((m = rx.exec(s)) !== null) {
    document.body.innerHTML += m[1] + ": " + m[2] + "<br/>";
}

Pattern explanation:

  • (aaa aa|bbbb|cccc) - either of aaa aa or bbbb or cccc
  • : - a literal colon
  • (.*?) - Group 2 matching 0+ any chararacter other than a newline as few as possible up to the first...
  • (?=$|\s+(?:aaa aa|bbbb|cccc)) - (a positive lookahead that limits the .*? matching)
    • $ - ... end of string
    • | - or...
    • \s+ - one or more whitespaces followed with...
      • (?:aaa aa|bbbb|cccc) - any of the three alternatives (inside a non-capturing group used for grouping only, not capturing)

Well so this will do what you want

/(aaa aa|bbbb|cccc)+:(\s*[a-zA-Z]*)(?:$|\s)/ig

But, it is highly contrived, given the question.

If you are trying to get just the labels (even if the key has a space), here's something I quickly typed up: https://regex101./r/jJ9iI1/3

(?:^|\s)([^:]+):

It starts off with a ^ or \s (beginning or space character). Then it simply captures every character (except the colon) leading up to a colon.

...make sure the g for global is turned on

发布评论

评论列表(0)

  1. 暂无评论