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

javascript - Regex match string before and after a specific character - Stack Overflow

programmeradmin1浏览0评论

I would like to make a grouping to select the strings before AND after a specific character, in this case it's the Colon.

Example:

First: foo Last: bar

I would like to Match First and foo in group 1 and Last bar in group 2

Group1: First foo

Group2: Last bar


I currently have

([^:]*)+([^:*])

Which only matches everything that's not a colon, which isn't exactly what i'm looking for. What are some ways or patterns with regex where i can match before and after a certain character?

I would like to make a grouping to select the strings before AND after a specific character, in this case it's the Colon.

Example:

First: foo Last: bar

I would like to Match First and foo in group 1 and Last bar in group 2

Group1: First foo

Group2: Last bar


I currently have

([^:]*)+([^:*])

Which only matches everything that's not a colon, which isn't exactly what i'm looking for. What are some ways or patterns with regex where i can match before and after a certain character?

Share Improve this question edited Feb 16, 2018 at 22:43 azro 54.2k9 gold badges38 silver badges73 bronze badges asked Feb 16, 2018 at 22:29 Veda99817Veda99817 1691 gold badge2 silver badges15 bronze badges 5
  • Try /([^:]+):\s*(.*?)(?=\w+:|$)/ – Wiktor Stribiżew Commented Feb 16, 2018 at 22:31
  • @WiktorStribiżew using a non capturing group to get just the required matches might be slightly better: (?:[^:]+):\s*(.*?)(?=\w+:|$) – Tibrogargan Commented Feb 16, 2018 at 22:33
  • I'm afraid you're going to have to use replace if you want to group them in one group – GalAbra Commented Feb 16, 2018 at 22:40
  • 1 @Tibrogargan Well, I am not really sure of the required output. It looks like Group 1 should be First foo and that is impossible since the texts are not continuous. See this JSFiddle, you may still use that regex above, but join the groups together. – Wiktor Stribiżew Commented Feb 16, 2018 at 22:40
  • You forgot to put that "certain character" in your regex. – axiac Commented Feb 17, 2018 at 13:31
Add a ment  | 

3 Answers 3

Reset to default 2

As you want the colon removed, but still have the surrounding text in one group, you'll need to use some string manipulation after executing the regular expression:

var s = "First: foo Last: bar",
    re = /\s*([^:]*?)\s*:\s*([^:\s]*)/g,
    result = [];
while (match = re.exec(s)) {
    result.push(match[1] + ' ' + match[2]);
}

console.log(result);

Note that it can be ambiguous which word belongs where, when there are more spaces, for example in First: foo hello there: bar.

The texts you want to get into single group is not a streak of continuous chars, thus, it is impossible to achieve. You need to grab the two parts, key and value separately, and then join them:

var rx = /([^:]+):\s*(.*?)(?=\w+:|$)/g;
var s = "First: foo Last: bar";
var m, res=[];
while (m=rx.exec(s)) {
  res.push([m[1].trim(),m[2].trim()].join(" "));
}
console.log(res);

See the regex demo at regex101.. Details:

  • ([^:]+) - Group 1: one or more chars other than :
  • : - a :
  • \s* - 0+ whitespaces
  • (.*?) - Group 2: any 0+ chars other than line break chars, as few as possible
  • (?=\w+:|$) - followed with 1+ word chars and : or end of string.

You can use the following regex:

([a-zA-Z]+:\s*[a-zA-Z]+) 

You can try it out on the link HERE.

'First: foo Last: bar'.match(/([a-zA-Z]+:\s*[a-zA-Z]+)/g) // ["First: foo", "Last: bar"]
发布评论

评论列表(0)

  1. 暂无评论