gpt4 book ai didi

javascript - 正则表达式仅在 "asdf:adsf"中查找冒号,但在 "asdf: adsf"中查找不到

转载 作者:行者123 更新时间:2023-11-30 17:06:22 30 4
gpt4 key购买 nike

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

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

\x20*:\x20* // wrong because this captures all whitespace

最佳答案

\S(:)(?=\S)

试试这个。捕获捕获或组。查看演示。

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.
}

关于javascript - 正则表达式仅在 "asdf:adsf"中查找冒号,但在 "asdf: adsf"中查找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27947357/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com