[英]Regex match string before and after a specific 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. 我想进行分组以选择特定字符在AND之前的字符串,在这种情况下为冒号。
Example: 例:
First: foo Last: bar
I would like to Match First and foo in group 1 and Last bar in group 2 我想匹配第1组中的First和foo和第2组中的Last bar
Group1: First foo
First foo
: First foo
Group2: Last bar
第2组: 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? 我可以在某个字符之前和之后匹配的正则表达式有哪些方式或模式?
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
. 请注意,当有更多空格时,例如在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.com . 请参阅regex101.com上的regex演示 。 Details: 细节:
([^:]+)
- Group 1: one or more chars other than :
([^:]+)
-组1:一个或多个除:
字符之外的字符:
:
- a :
:
-一个:
\\s*
- 0+ whitespaces \\s*
-0+空格 (.*?)
- Group 2: any 0+ chars other than line break chars, as few as possible (.*?)
-第2组:除换行符以外的任何0+个字符,且尽可能少 (?=\\w+:|$)
- followed with 1+ word chars and :
or end of string. (?=\\w+:|$)
-后跟1+个单词字符和:
或字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.