views:

75

answers:

2

Whats the easiest way in javascript to replace ABC, DEF, GHI with XYZ in the following strings

http://z.site.com/z/ABC/z/z.html
http://z.site.com/z/DEF/z/z.html
http://z.site.com/z/GHI/z/z.html
+4  A: 
var url = "http://z.site.com/z/ABC/z/z.html";
url = url.replace(/ABC|DEF|GHI/, "XYZ");
mwilcox
ahh, you red it before i edited.
acidzombie24
Funny, there are 0 views on this page.
acidzombie24
+1  A: 

You can use a regular expression to detect that pattern and change the three letters in question:

s = s.replace(/^http:\/\/z\.site\.com\/z\/.../, 'http://z.site.com/z/XYZ');
Jason Orendorff
I use $1 to get the beginning since the subdomain and the first /z MAY change.
acidzombie24