javascript - RegEx find unique words -


i'm trying parse string in javascript , find unique words, starts : symbol. wrote regular expression purpose:

/(:[a-z]\w+)(?!.*\1)/g 

it works fine string:

"test :one :one test :one test :two".match(/(:[a-z]\w+)(?!.*\1)/g) 

and result [':one', ':two'] online example #1

but, if after word goes new line symbol

"test :one\n :one test :one test :two".match(/(:[a-z]\w+)(?!.*\1)/ig) 

regex not working , returns [':one', ':one', ':two'] online example #2

how modify regex , unique results?

you need use [\s\s] instead of . make sure check may go far end of string (not line) , [a-za-z] instead of [a-z] (see why regex allowing caret?):

/(:[a-z]\w+)(?![\s\s]*\1)/gi 

see regex demo

var re = /(:[a-z]\w+)(?![\s\s]*\1)/gi;   var str = 'test :one\n :one test :one test :two';  console.log(str.match(re));  //or, rid of inital :  console.log(str.match(re).map(function(x){return x.substr(1);}));


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -