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+'\\$');

更多相关文章

  1. 密码强度正则表达式与数字[重复]
  2. 正则表达式匹配所有字符到某个标记
  3. JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
  4. java 如何获取动态网页内容,返回字符串
  5. 粗见之正则表达式
  6. NodeJS - 解析JSON(只有字符串或数字)
  7. 如何在JavaScript中对字符串排序
  8. JavaScript表单验证和正则表达式
  9. JSON.parse(xhr.responseText)意外的字符串错误

随机推荐

  1. android shape用法
  2. Android内存管理机制
  3. Android 4.0.3 CTS 测试
  4. android系统工具之 draw9patch超详细教程
  5. Spinner的三级联动
  6. ExpandableListView 怎么和其他组件相互
  7. Android(安卓)-- 动态添加布局
  8. android命令行播放mp3
  9. Android补间动画原理介绍
  10. Android 软键盘功能键(EditText)