在Javascript中组合正则表达式 [英] Combining regular expressions in Javascript
本文介绍了在Javascript中组合正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在javascript中组合正则表达式。
Is it possible to combine regular expressions in javascript.
例如:
var lower = /[a-z]/;
var upper = /[A-Z]/;
var alpha = upper|lower;//Is this possible?
ie。我可以将正则表达式分配给变量,并使用模式匹配字符将这些变量组合为
我们在正则表达式中进行
ie. can i assign regular expressions to variables and combine those variables using pattern matching characters as we do in regular expressions
推荐答案
答案是肯定的!你必须在RegExp类下初始化变量:
The answer is yes! You have to initialize the variable under the RegExp class:
var lower = new RegExp(/--RegexCode--/);
var upper = new RegExp(/--RegexCode--/);
因此,可以动态创建正则表达式。创建后:
hence, regex can be dynamically created. After creation:
"sampleString".replace(/--whatever it should do--/);
然后你可以正常组合它们,是的。
Then you can combine them normally, yes.
var finalRe = new RegExp(lower.source + "|" + upper.source);
这篇关于在Javascript中组合正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文