I´ve got the following string from a .getAttribute():
ctl00$m$g_ff7ec6ac_ec2e_4402_aaaa_7fcce245ff1b$ctl03$_UserName
Let´s call it “String”. Now I need to replace the $ to a _ . I tried:
String.replace(/\$/g, "\_");
… doesn´t work.
Second try:
String.replace(/$$/g, "\_");
… doesn´t work.
Third try:
String.replace(/\$$/g, "\_");
… doesn´t work.
So… Ca anyone help? Thanks for all effort!
EDIT: Need to get it work for IE8/9
The code at this time:
mailName = document.body.innerHTML.match(/ctl00\$.+EmailAddress/);
alert(mailName); // String is "ctl00$m$g_ff7ec6ac_ec2e_4402_aaaa_7fcce245ff1b$ctl03$_UserName"
mailName2 = mailName.replace(/\$/g, "_");
EDIT2:
… I´va got the answer one my own.
mailName = document.body.innerHTML.match(/ctl00\$.+EmailAddress/);
gives a string back but .replace() or .split() won´t work with it. To get ot work you need to do it like this
mailID = '"' + document.body.innerHTML.match(/ctl00\$.+EmailAddress/) + '"';
after that everything is fine. Don´t know why, but in IE8/9 this solution works great.
Use
No need to escape the second string.
From the MDN :
Note also that replace doesn’t change the original string. You need to get back the returned value.
And, regarding your edit, note that the match function of Internet Explorer seems to return an array even without the
gmodifier :You can do
Demonstration