我试图用regex来捕获类似asdf:adsf的冒号,它也可能看起来像a:b、asdf:b、b:asdf、123:b、2:1等等。但是,它不应该与asdf: adsf、a: b、asdf :b、b: asdf、123 : b、2: 1相匹配,
到目前为止,我已经尝试了一系列不同的正则表达式,而我最近的尝试是:
\x20*:\x20* // wrong because this captures all whitespace发布于 2015-01-14 16:09:24
\S(:)(?=\S)尝试this.Grab捕获或group.See演示。
https://regex101.com/r/fA6wE2/29
var re = /\S(:)(?=\S)/gm;
var str = 'a:b\nasas:b\na:sadasd\na: sdffds\na :asfsd\na : dsfdf';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}发布于 2015-01-14 18:56:58
\w+(?=\S):(?=\S)\w+这将解决问题。
我的另一个答案被删除了,因为之前发布的那个regex。
https://stackoverflow.com/questions/27947357
复制相似问题