1

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

2 Answers 2

4
var url = "http://z.site.com/z/ABC/z/z.html";
url = url.replace(/ABC|DEF|GHI/, "XYZ");
Sign up to request clarification or add additional context in comments.

1 Comment

Funny, there are 0 views on this page.
1

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');

1 Comment

I use $1 to get the beginning since the subdomain and the first /z MAY change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.