正则表达式在javascript中查找某个内容之间的字符串
我有两个字符串:正则表达式在javascript中查找某个内容之间的字符串,javascript,regex,Javascript,Regex,我有两个字符串: (username~contains~'ren'~and~status~contains~false) (status~contains~false~and~username~contains~'ren') 我需要一个正则表达式来查找包含后的状态值~ 我试着做如下事情: /(?<=status~contains~).*?(?=[)])|(?<=status~contains~).*?(?=~)/gi /(?您可以更改的内容是匹配~或) \(匹配开口( (?:非
(username~contains~'ren'~and~status~contains~false)
(status~contains~false~and~username~contains~'ren')
我需要一个正则表达式来查找包含后的状态值~
我试着做如下事情:
/(?<=status~contains~).*?(?=[)])|(?<=status~contains~).*?(?=~)/gi
/(?您可以更改的内容是匹配~
或)
\(
匹配开口(
(?:
非捕获组
[^~\r\n]+~
匹配除~
或换行符以外的任何字符的1+倍
)*
关闭goup,重复0多次
status~包含~
Match字面意思
(
Capturegroup 1
[^~\n]+
匹配除~
或换行符以外的任何字符的1+倍
)
关闭组
(?=[^()]*\)
正向前瞻,断言结束)
const regex=/\((?:[^-\r\n]+~)*status~包含([^-\n]+)(?=[^()]*\)/g;
常量str=`(username~包含~'ren'~和~status~包含~false)
(状态~contains~false~和~username~包含~'ren')`;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
console.log(m[1]);
}
试试这个正则表达式:
/(?<=status~contains)~([^~)]+)[~)]/
/(?True或false/^(?=.*status~包含~(True | false))./
使用多行选项,组1为值
第一个示例看起来不错,但第二个没有查找功能的示例无法通过,请在注册表中进行测试site@Jhonny在第二个示例中,值位于第一个捕获组中。这是一个选择了不同引擎的示例,该引擎将以绿色突出显示组。您可以使用示例Javascript获得这些匹配,而不使用look哦,谢谢你的解释。我会接受你的回答,因为这是解释得最多的
\((?:[^~\r\n]+~)*status~contains~([^~\n]+)(?=[^()]*\))
/(?<=status~contains)~([^~)]+)[~)]/
/(?<=status~contains~)[^~)]+(?=[~)])/g