I am trying to replace a string
var url = "www.fnord.com/foo/bar/btn_close_white.gif";
Simple word for word for word replacement works fine:
var newUrl = url.replace(/btn_close_white.gif/g,"btn_close_black.gif");
Yet since I do not know what color is currently set, I need the regex to ignore the color part and replace the whole segment. So that in theory, btn_close_pink.gif or btn_close_someOtherFancyColor.gif all get replaced to btn_close_black.gif.
I tried this, but it fails and I do not know why:
var newUrl = url.replace(/btn_close_*.gif/g,"btn_close_black.gif"); // this fails
What am I doing wrong?
Try this:
Regexp
[\w\d]*means any amount of letters or digits. If you allow any characters in url do this:EDIT: When your write
"/btn_close_*\.gif/"regexp this means that your string should contain “btn_close” string + any amount of “_” character + “.gif” string.When your write
"/btn_close_.*\.gif/"regexp this means that your string should contain “btn_close” string + any amount of any characters + “.gif” string.