我试图使用正则表达式来匹配两个字符之间的内部文本,但是我得到了错误的文本
我尝试放置[Az] *而不是。*来仅匹配内部文本,并且可以正常工作。但是我也需要匹配非字母字符。
/\[?(,? ?\[(\[(.+)-(.+)\])\])\]?/g
这是我的正则表达式,我想匹配方括号之间的字符:
[[[ hello-hello]],[[hi -hi]]]
粗体字符是匹配的字符。
我期望匹配[[[您好,您好],[喜喜]]]在比赛1和[[[您好,您好],[喜喜]]]在比赛两项。
如果[]
期望介于两者之间的所有内容,那么我们可以将表达式简化为:
(?:\[+)(.+?)(?:\]+)
在这里,我们在此捕获组中捕获了可能需要的子字符串:
(.+?)
然后,我们使用两个非捕获组在其左侧和右侧添加两个边界:
(?:\[+)
(?:\]+)
const regex = /(?:\[+)(.+?)(?:\]+)/g;
const str = `[[[hello-hello]]
[[hi-hi]]]
[[hi hi]]]`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
如果不需要此表达式,则可以在regex101.com中对其进行修改/更改。
jex.im可视化正则表达式:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句