RegExp and special characters

巧了我就是萌 提交于 2019-12-24 07:31:14

问题


I need to use regexp for matching and the code below works fine. However, I need to KEEP the dollar sign ($) as a true dollar sign and not a special character.

I've tried excluding but nothing is working.

IE:  [^$]

Here's the code. It works as expected except when the text contains a $ or IS the $.

textNode = "$19,000"; 
regex = RegExp("$19,000",'ig');
text = '$';

textReplacerFunc: function (textNode, regex, text) {
        var sTag = '<span class="highlight">';
        var eTag = '</span>';
        var re = '(?![^<>]*>)(' + text + '(?!#8212;))';
        var regExp = new RegExp(re, 'ig');
        textNode.data = textNode.data.replace(regExp, sTag + '$1' + eTag);
    },

RESULT: $ not highlighted. desired results: $19,000


回答1:


Make sure to double escape the $ as in :

text = '\\$';

Since you are using construction of RegExp instance using a string here.



来源:https://stackoverflow.com/questions/23349041/regexp-and-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!