0
var src = "http://blah.com/SOMETHING.jpg";
src.replace(/.*([A-Z])\.jpg$/g, "X");

at this point, shouldn't src be:

http://blah.com/SOMETHINX.jpg

If I use match() with the same regular expression, it says it matched. Regex Coach also shows a match on the character "G".

Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106

3 Answers3

4

Try

src = src.replace(/.*([A-Z])\.jpg$/g, "X");

String#replace isn't a mutator method; it returns a new string with the modification.

EDIT: Separately, I don't think that regexp is exactly what you want. It says "any number of any character" followed by a captured group of one character A-Z followed by ".jpg" at the end of the string. src becomes simply "X".

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

The replace function doesn't change src.

I think what you want to do is:

src = src.replace(/.*([A-Z])\.jpg$/g, "X");
Jimmy Stenke
  • 11,140
  • 2
  • 25
  • 20
1

src.replace will replace the entire match "http://blah.com/SOMETHING.jpg", not just the part you captured with brackets.

Igor ostrovsky
  • 7,282
  • 2
  • 29
  • 28