views:

201

answers:

3
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".

+2  A: 

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
Beat Jimmy by 10 seconds :)
Daniel
@Daniel -- LOL....
T.J. Crowder
+1  A: 

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
+1  A: 

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

Igor ostrovsky
This is mostly to blame to my wrong testcase, but +1 for correcting me on it anyway :P
Daniel