gpt4 book ai didi

javascript - 如何匹配不在括号和引号中的文本?

转载 作者:行者123 更新时间:2023-11-29 20:51:37 24 4
gpt4 key购买 nike

我有这样一个字符串:

this text (an another text) "and a text" and again a text

我想要的是只匹配第一个text。其他两个 text 单词要么在括号内,要么在引号中。

this text (an another text) "and a text" and again a text
^ this is I do want to capture. and this -> ^

如何在单个正则表达式匹配中执行此操作?我无法在一场比赛中为这两种情况找到任何解决方案。

文本可以任意顺序。

最佳答案

var sTest = 'this text (an another text) "and a text"';
document.writeln(sTest.replace(/\([^)]*text[^)]*\)|"[^"]*text[^"]*"|text/g, (sMatch)=>{ return (sMatch === 'text' ? 'TEXT' : sMatch); }));

使用 The Best Regex Trick .您先定义不想包含的内容,然后只剩下您想要的内容。

关于javascript - 如何匹配不在括号和引号中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51572077/

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