Javascript正则表达式对象和美元符号
16lz
2021-01-22
In the code below. I expected true but i am getting false instead. What am I missing?
在下面的代码中。我期待的是真的,但我变得虚假了。我错过了什么?
var text = "Sentence $confirmationlink$ fooo";
alert(placeHolderExists(text,'confirmationlink'); // alerts false
function placeHolderExists(text,placeholdername) {
var pattern = new RegExp('\$'+placeholdername+'\$');
return pattern.test(text);
}
4 个解决方案
#1
The "\" in the RegExp expression builder is treated as an escape character when building the string, just as it is in the actual RegExp. You need to escape twice, try:
构建字符串时,RegExp表达式构建器中的“\”被视为转义字符,就像在实际的RegExp中一样。你需要逃脱两次,尝试:
new RegExp('\\$'+placeholdername+'\\$');
更多相关文章
- 密码强度正则表达式与数字[重复]
- 正则表达式匹配所有字符到某个标记
- JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
- java 如何获取动态网页内容,返回字符串
- 粗见之正则表达式
- NodeJS - 解析JSON(只有字符串或数字)
- 如何在JavaScript中对字符串排序
- JavaScript表单验证和正则表达式
- JSON.parse(xhr.responseText)意外的字符串错误