简体   繁体   中英

Matching whole words with Javascript's Regex with a few restrictions

I am trying to create a regex that can extract all words from a given string that only contain alphanumeric characters.

Yes
yes absolutely
#no
*NotThis
orThis-- 
Good *Bad*
1ThisIsOkay2 ButNotThis2) 

Words that should have been extracted: Yes, yes, absolutely, Good, 1ThisIsOkay2

Here is the work I have done thus far:

/(?:^|\b)[a-zA-Z0-9]+(?=\b|$)/g

I had found this expression that works in Ruby ( with some tweaking ) but I have not been able to convert it to Javascript regex.

Use /(?:^|\\s)\\w+(?!\\S)/g to match 1 or more word chars in between start of string/whitespace and another whitespace or end of string:

 var s = "Yes\\nyes absolutely\\n#no\\n*NotThis\\norThis-- \\nGood *Bad*\\n1ThisIsOkay2 ButNotThis2)"; var re = /(?:^|\\s)\\w+(?!\\S)/g; var res = s.match(re).map(function(m) { return m.trim(); }); console.log(res); 

Or another variation:

 var s = "Yes\\nyes absolutely\\n#no\\n*NotThis\\norThis-- \\nGood *Bad*\\n1ThisIsOkay2 ButNotThis2)"; var re = /(?:^|\\s)(\\w+)(?!\\S)/g; var res = []; while ((m=re.exec(s)) !== null) { res.push(m[1]); } console.log(res); 

Pattern details :

  • (?:^|\\s) - either start of string or whitespace (consumed, that is why trim() is necessary in Snippet 1)
  • \\w+ - 1 or more word chars (in Snippet 2, captured into Group 1 used to populate the resulting array)
  • (?!\\S) - negative lookahead failing the match if the word chars are not followed with non-whitespace.

You can do that (where s is your string) to match all the words:

var m = s.split(/\s+/).filter(function(i) { return !/\W/.test(i); });

If you want to proceed to a replacement, you can do that:

var res = s.split(/(\s+)/).map(function(i) { return i.replace(/^\w+$/, "@");}).join('');

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