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,338
  • 14
  • 69
  • 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
  • 959,406
  • 173
  • 1,780
  • 1,769
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,090
  • 2
  • 24
  • 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,172
  • 2
  • 28
  • 28