user34537
user34537

Reputation:

Easiest way to replace a substring in javascript

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

Upvotes: 1

Views: 438

Answers (2)

Jason Orendorff
Jason Orendorff

Reputation: 45116

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

Upvotes: 1

mwilcox
mwilcox

Reputation: 4132

var url = "http://z.site.com/z/ABC/z/z.html";
url = url.replace(/ABC|DEF|GHI/, "XYZ");

Upvotes: 4

Related Questions