this is not what i'm asking for:
这不是我想要的:
concatenate multiple regexes into one regex
将多个regexe连接到一个regex中
is there a way to append a regex into another one (in javascript language) ?
是否有方法将regex附加到另一个(用javascript语言)中?
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:
举个例子,在python中,我会这样写:
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.
但是在javascript中,正则表达式不是字符串。
re = /^animal: (rami|shmulik|dudu)$/;
or am i missing something?
还是我漏掉了什么?
2 个解决方案
#1
5
You don't have to use the literal notation. You could instead create a new RegExp object.
你不需要用文字符号。您可以创建一个新的RegExp对象。
var myRegex = new RegExp("^animal: (rami|shmulik|dudu)$");
#2
2
var regstr1 = 'dogs|cats|mice',
regstr_container = '^animal: (cows|sheep|'+ regstr1 +'|zebra)$',
regex = RegExp(regstr_container);
#1
5
You don't have to use the literal notation. You could instead create a new RegExp object.
你不需要用文字符号。您可以创建一个新的RegExp对象。
var myRegex = new RegExp("^animal: (rami|shmulik|dudu)$");
#2
2
var regstr1 = 'dogs|cats|mice',
regstr_container = '^animal: (cows|sheep|'+ regstr1 +'|zebra)$',
regex = RegExp(regstr_container);