正则表达式仅在 "asdf:adsf" 中查找冒号而不在 "asdf: adsf" 中查找

Regex to find only colon in "asdf:adsf" but not "asdf: adsf"

我正在尝试使用正则表达式仅捕获像 asdf:adsf 这样的冒号,它也可能看起来像 a:basdf:bb:asdf123:b2:1 等。但是,它不应匹配 asdf: adsfa: basdf :bb: asdf123 : b2: 1,

到目前为止,我已经尝试了很多不同的正则表达式,最接近的是:

\x20*:\x20* // wrong because this captures all whitespace
\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.
}
\w+(?=\S):(?=\S)\w+

这将解决问题。

我的另一个答案被之前发布的那个正则表达式删除了。