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); }
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: