RegExp and special characters

2019-08-12 07:57发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-08-12 08:35

Make sure to double escape the $ as in :

text = '\\$';

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

查看更多
登录 后发表回答