简体   繁体   中英

regex to match all words but AND, OR and NOT

In my javascript app I have this random string:

büert AND NOT 3454jhadf üasdfsdf OR technüology AND (bar OR bas)

and i would like to match all words special chars and numbers besides the words AND , OR and NOT .

I tried is this

/(?!AND|OR|NOT)\\b[\À-\ſ\\w\\d]+/gi
which results in
["büert", "3454jhadf", "asdfsdf", "technüology", "bar", "bas"]

but this one does not match the ü or any other letter outside the az alphabet at the beginning or at the end of a word because of the \\b word boundary.

removing the \\b oddly ends up matching part or the words i would like to exclude:

/(?!AND|OR|NOT)[\À-\ſ\\w\\d]+/gi
result is
["büert", "ND", "OT", "3454jhadf", "üasdfsdf", "R", "technüology", "ND", "bar", "R", "bas"]

what is the correct way to match all words no matter what type of characters they contain besides the ones i want exclude?

The issue here has its roots in the fact that \\b (and \\w , and other shorthand classes) are not Unicode-aware in JavaScript.

Now, there are 2 ways to achieve what you want.

1. SPLIT WITH PATTERN(S) YOU WANT TO DISCARD

 var re = /\\s*\\b(?:AND|OR|NOT)\\b\\s*|[()]/; var s = "büert AND NOT 3454jhadf üasdfsdf OR technüology AND (bar OR bas)"; var res = s.split(re).filter(Boolean); document.body.innerHTML += JSON.stringify(res, 0, 4); // = > [ "büert", "3454jhadf üasdfsdf", "technüology", "bar", "bas" ] 

Note the use of a non-capturing group (?:...) so as not to include the unwanted words into the resulting array. Also, you need to add all punctuation and other unwanted characters to the character class.

2. MATCH USING CUSTOM BOUNDARIES

You can use groupings with anchors/reverse negated character class in a regex like this:

(^|[^\u00C0-\u017F\w])(?!(?:AND|OR|NOT)(?=[^\u00C0-\u017F\w]|$))([\u00C0-\u017F\w]+)(?=[^\u00C0-\u017F\w]|$)

The capure group 2 will hold the values you need.

See regex demo

JS code demo:

 var re = /(^|[^\À-\ſ\\w])(?!(?:AND|OR|NOT)(?=[^\À-\ſ\\w]|$))([\À-\ſ\\w]+)(?=[^\À-\ſ\\w]|$)/gi; var str = 'büert AND NOT 3454jhadf üasdfsdf OR technüology AND (bar OR bas)'; var m; var arr = []; while ((m = re.exec(str)) !== null) { arr.push(m[2]); } document.body.innerHTML += JSON.stringify(arr); 

or with a block to build the regex dynamically:

 var bndry = "[^\\\À-\\\ſ\\\\w]"; var re = RegExp("(^|" + bndry + ")" + // starting boundary "(?!(?:AND|OR|NOT)(?=" + bndry + "|$))" + // restriction "([\\\À-\\\ſ\\\\w]+)" + // match and capture our string "(?=" + bndry + "|$)" // set trailing boundary , "g"); var str = 'büert AND NOT 3454jhadf üasdfsdf OR technüology AND (bar OR bas)'; var m, arr = []; while ((m = re.exec(str)) !== null) { arr.push(m[2]); } document.body.innerHTML += JSON.stringify(arr); 

Explanation:

  • (^|[^\À-\ſ\\w]) - our custom boundary (match a string start with ^ or any character outside the [\À-\ſ\\w] range)
  • (?!(?:AND|OR|NOT)(?=[^\À-\ſ\\w]|$)) - a restriction on the match: the match is failed if there are AND or OR or NOT followed by string end or characters other than those in the \À-\ſ range or non-word character
  • ([\À-\ſ\\w]+) - match word characters ( [a-zA-Z0-9_] ) or those from the \À-\ſ range
  • (?=[^\À-\ſ\\w]|$) - the trailing boundary, either string end ( $ ) or characters other than those in the \À-\ſ range or non-word character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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