简体   繁体   English

正则表达式在 javascript 中的某物之间查找字符串

[英]Regex to find a string between something in javascript

I have this two strings:我有这两个字符串:

(username~contains~'ren'~and~status~contains~false)
(status~contains~false~and~username~contains~'ren')

i need one regex that find what is the status value after contains~我需要一个正则表达式来查找包含后的状态值是什么~

I try something like:我尝试类似:

/(?<=status~contains~).*?(?=[)])|(?<=status~contains~).*?(?=~)/gi

pass in exemple 1: (username~contains~'ren'~and~status~contains~false)传入例1: (username~contains~'ren'~and~status~contains~false)
fail in exemple 2: (status~contains~false~and~username~contains~'ren' )在示例 2 中失败: (status~contains~false~and~username~contains~'ren'

I need only the return true or false, but in the second exemple i get false~and~username~contains~'ren'我只需要返回 true 或 false,但在第二个示例中我得到false~and~username~contains~'ren'

True or false /^(?=.*status~contains~(true|false)).*/真假/^(?=.*status~contains~(true|false)).*/
Use the multi-line option and group 1 is the value.使用多行选项,组 1 是值。

https://regex101.com/r/N41U56/1 https://regex101.com/r/N41U56/1

The thing that you could change is to match either a ~ or )您可以更改的是匹配~)

(?<=status~contains~).*?(?=[)~])

Regex demo正则表达式演示

Instead of using a lookbehind (which is not yet fully supported across browsers), you could also use a capturing group instead and make sure there are opening and closing parenthesis.除了使用lookbehind(浏览器尚不完全支持),您还可以使用捕获组,并确保有左括号和右括号。

The value is in group 1.该值在第 1 组中。

\((?:[^~\r\n]+~)*status~contains~([^~\n]+)(?=[^()]*\)) 
  • \( Match an opening ( \(匹配一个开口(
  • (?: Non capturing group (?:非捕获组
    • [^~\r\n]+~ Match 1+ times any char except ~ or a newline [^~\r\n]+~匹配除~或换行符以外的任何字符 1+ 次
  • )* Close goup, repeat 0+ times )*关闭组,重复 0+ 次
  • status~contains~ Match literally status~contains~从字面上匹配
  • ( Capture group 1 (捕获组 1
    • [^~\n]+ Match 1+ times any char except ~ or a newline [^~\n]+匹配除~或换行符之外的任何字符 1+ 次
  • ) Close group )关闭组
  • (?=[^()]*\)) Positive lookahead, assert a closing ) (?=[^()]*\))正向前瞻,断言结束)

Regex demo正则表达式演示

 const regex = /\((?:[^~\r\n]+~)*status~contains~([^~\n]+)(?=[^()]*\))/g; const str = `(username~contains~'ren'~and~status~contains~false) (status~contains~false~and~username~contains~'ren')`; let m; while ((m = regex.exec(str)).== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex;lastIndex++. } console;log(m[1]); }

Try this regex:试试这个正则表达式:

/(?<=status~contains)~([^~)]+)[~)]/

and get group 1 from result.并从结果中获取第 1 组。

/(?<=status~contains~)[^~)]+(?=[~)])/g

This uses a lookbehind of: status~contains~ and a lookahead of either ~ or ) .这使用后视: status~contains~和前瞻~) Therefore we can match 1 or more characters that are neither ~ nor ) between the lookbehind and lookahead.因此,我们可以在后向和前向之间匹配 1 个或多个既不是~也不是)的字符。 This is simpler and more efficient than using .*?这比使用.*?更简单、更高效. .

 let re = /(?<=status~contains~)[^~)]+(?=[~)])/g; let text = "(username~contains~'ren'~and~status~contains~false)" + "(status~contains~false~and~username~contains~'ren')"; let m; while (m = re.exec(text)) console.log(m[0]);

See Regex Demo见正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM