D0dger D0dger - 10 months ago 130
Javascript Question

RegEx find unique words

I'm trying to parse string in JavaScript and find all unique words, which starts with

symbol. I wrote that regular expression for this purpose:


It works fine for this string:

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

And result is
[':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 properly and returns
[':one', ':one', ':two']
Online example #2

How to modify this regex and get only unique results?


You need to use [\s\S] instead of . to make sure the check may go as far as the end of string (not just line) and [a-zA-Z] instead of [A-z] (see Why is this regex allowing a caret?):


See the regex demo

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