
Originally Posted by
s_sameer
var objRegExp = /$/g;
v1=v1.replace(objRegExp,"");
--------
and it will remove $ form the var v1
As it stands, that won't do anything as the regular expression will match any string (but nothing in particular within it). The dollar ($) symbol is an end-of-string assertion (or end-of-line with the m flag). You need to prefix it with a backslash to make it a literal dollar:
  /\$/g

Originally Posted by
jscheuer1
eval('var objRegExp = /'+v1.charAt(i)+'/g');
Good grief, no!
I can guarantee that anytime you think you need to use the eval function, you're missing a much simpler, quicker, better alternative. In this case, the OP should use the RegExp constructor function. In its simplest form, this takes two arguments: the pattern and optional flags (both are strings). The previous object could be constructed with:
Code:
new RegExp('\\$', 'g')
Note that because I'm using a string literal, the backslash that escapes the dollar symbol needs to be escaped itself (this would be true with eval as well). So, for the OP, we'd have:
Code:
new RegExp('\\' + v1.charAt(i), 'g')
or in full:
Code:
v1 = v1.replace(new RegExp('\\' + v1.charAt(i), 'g'), '')
Hope that helps,
Mike
Bookmarks