regex - Javascript Regexp dynamic generation from variables? -


how construct 2 regex patterns one?

for example have 1 long pattern , 1 smaller, need put smaller 1 in front of long one.

var pattern1 = ':\(|:=\(|:-\('; var pattern2 = ':\(|:=\(|:-\(|:\(|:=\(|:-\(' str.match('/'+pattern1+'|'+pattern2+'/gi'); 

this doesn't work. when i'm concatenating strings, slashes gone.

you have use regexp:

str.match(new regexp(pattern1+'|'+pattern2, 'gi')); 

when i'm concatenating strings, slashes gone.

if have backslash in pattern escape special regex character, (like \(), have use two backslashes in string (because \ escape character in string): new regexp('\\(') same /\(/.

so patterns have become:

var pattern1 = ':\\(|:=\\(|:-\\('; var pattern2 = ':\\(|:=\\(|:-\\(|:\\(|:=\\(|:-\\('; 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -