D0dger D0dger - 2 months ago 25
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:

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


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?

Answer

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?):

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

See the 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, get rid of the inital :
console.log(str.match(re).map(function(x){return x.substr(1);}));