I have a problem to do a replace in javascript with regex
this is the code
var link = document.getElementsByTagName('a');
var i = link.length;
while(i--){
var lnk = link[i].href;
lnk.replace('/^http\:\/\/www\.ringtonematcher\.com\/go\/\?sid\=([\w]+)\&/g', 'test');
}
sid=replace& = sid=test&
I guess your problem is that you don’t do anything with the result of the
replacemethod. It does not alter anything, but returns a new string. If you’d want to change thehrefattributes, you will need to reassign it:I also fixed some flaws in your regex:
:,=or&characters\win a character classYou don’t want the\w+to be greedy