本文介绍了新的Regexp不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将以下表达式转换为new Regexp()样式:
I'm trying to convert following expression to "new Regexp()" style:
var Wyrazenie = /\btest[a-z]*/g;
我真的很困惑,不知道如何修复它。以下是我所做的,但显然它不起作用。
I'm really confused with it and have no idea how to fix it. Below is what I've done but obviously it doesn't work.
var Wyraznie = new RegExp("\btest[a-z]*","g");
还有一个问题,如果不是测试,我会使用变量吗?
Also have a question how would it look if instead of "test" I would use variable?
推荐答案
你应该改用它......
You should use this instead...
new RegExp("\\btest[a-z]*", "g");
...如 \b
将当JavaScript解析器通过相应的字符串文字工作时,可以插入到单(无斜线)字符中。解决方案是逃避斜线本身。
... as \b
will be interpolated into a single (slashless) character when JavaScript parser works through the corresponding string literal. The solution is to escape slash itself.
DEMO:
这篇关于新的Regexp不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!