最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find logic operators in string with regex? - Stack Overflow

programmeradmin0浏览0评论

Lets say logic expression looks like this:

(A || B) && (C | D)

I would like to write regular expression that searches for both || and | characters inside the expression.

This is regex I currently use to find most of the other logic operators:

/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!/gi

So how to update currently looking regex to search for these two operators also?

Please note that it must be in similar form like it is now with operators divided with | sign (thats where my troubles e from i guess).

I tried doing it like this:

/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\/[|]\/|\/(|)*\//gi

But it doesn't matches my logic expression.

Lets say logic expression looks like this:

(A || B) && (C | D)

I would like to write regular expression that searches for both || and | characters inside the expression.

This is regex I currently use to find most of the other logic operators:

/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!/gi

So how to update currently looking regex to search for these two operators also?

Please note that it must be in similar form like it is now with operators divided with | sign (thats where my troubles e from i guess).

I tried doing it like this:

/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\/[|]\/|\/(|)*\//gi

But it doesn't matches my logic expression.

Share Improve this question asked Mar 28, 2017 at 20:19 Ognj3nOgnj3n 7598 silver badges30 bronze badges 6
  • Try /AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\|{1,2}/gi – Wiktor Stribiżew Commented Mar 28, 2017 at 20:21
  • Doesn't match those characters :S – Ognj3n Commented Mar 28, 2017 at 20:24
  • Really? Please show the code, there must be a problem somewhere else. – Wiktor Stribiżew Commented Mar 28, 2017 at 20:26
  • Just tack on |\|\||\| and you're done – user557597 Commented Mar 28, 2017 at 20:26
  • @Wiktor Stribiżew left few whitespaces after expresion on same online tool so it could not find mathc.. works, thanks :) – Ognj3n Commented Mar 28, 2017 at 20:28
 |  Show 1 more ment

2 Answers 2

Reset to default 3

try this:

AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|[||]{2}|\|

Also you can test out these regexes, over here: http://regexr./

You may add an alternative branch like |\|{1,2} to the regex of yours:

/AND|OR|and|or|XOR|&&|<=|>=|<|>|!=|==|&|OR*|!|\|{1,2}/gi

See the regex demo.

The \|{1,2} matches 2 or 1 (since {1,2} is a greedy version of a limiting quantifier, it will try to match as many | as it can) | symbols (this | must be escaped since otherwise, it is an alternation operator in a regex).

If you want to enhance it (get rid of redundancy), use

/and|x?or|&&|[<>!=]=|[<>&!]|\|{1,2}/gi

See another regex demo. Note that the words might need to be matched as whole words only, and then you'd need /\b(?:and|x?or)\b|&&|[<>!=]=|[<>&!]|\|{1,2}/gi.

发布评论

评论列表(0)

  1. 暂无评论