逃避正则表达式字符串? [英] Escape regexp strings?
本文介绍了逃避正则表达式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我记得有一种方法可以让一个非常长的字符串Regex逃脱。
I recall there was a way to have a very long string Regex escaped.
我认为我使用了新的Regexp但是我不记得怎么做了。
I think I used new Regexp but I can't recall how to do it.
这里的任何人都知道怎么做?
Anyone here who knows how?
推荐答案
建议你转过来使用新的RegExp(
,转换为正则表达式对象,如下所示:
Suggest you turn it into a string and then convert to a regex object using new RegExp(
, as shown below:
var rxstr = "[\d]" +
"[\w]";
var rx = new RegExp(rxstr);
或者您可以尝试(以格式为代价):
Or you could try (at the expense of formatting):
var x = "test";
var rxstr = "[\d]\
[\w]";
var rx = new RegExp(rxstr);
alert(rx);
后者更快,因为它不会创建一些新的字符串,但可能会被忽视。
The latter is faster as it does not create a number of new strings, but could be agued to be less readable.
这篇关于逃避正则表达式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文