views:

185

answers:

2

this is not what i'm asking for:

http://stackoverflow.com/questions/680446/concatenate-multiple-regexes-into-one-regex/680454

is there a way to append a regex into another one (in javascript language) ?

the reason for doing so is to simplify code maintenance (e.g. if the included string is long or easier to maintain by a non-programmer).

in python, for example, i'd write something like:

regstr1 = 'dogs|cats|mice'
regstr_container = '^animal: (cows|sheep|%s|zebra)$' % regstr1
re.compile(regstr_container)

however in javascript the regular expression is not a string.

re = /^animal: (rami|shmulik|dudu)$/;

or am i missing something?

+5  A: 

You don't have to use the literal notation. You could instead create a new RegExp object.

var myRegex = new RegExp("^animal: (rami|shmulik|dudu)$");
Cory Larson
+1, and you can make a placeholder: container = "...(cows|sheep|{BLAH}|zebra).."; var re = new Regexp(container.replace("{BLAH}", regstr1);
orip
+1  A: 
var regstr1 = 'dogs|cats|mice',
regstr_container = '^animal: (cows|sheep|'+ regstr1 +'|zebra)$',
regex = RegExp(regstr_container);
Eli Grey