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
Post a Comment