-1

I'm trying to figure out the best way to replace a string between 2 other strings. I believe regex is necessary for this.

Input string: "http://domainabc.com/dir1/dir2"

Output string: "http://domainxyz.com/dir1/dir2"

Only the domain will change - not the subdirectories.

4

1 回答 1

1

可能您希望更改当前域名而不关心域是什么。试试这个代码:

var s = "http://domainabc.com/dir1/dir2";
repl = s.replace(/\b(https?:\/\/)[^/]+(.+)$/, "$1domainxyz.com$2");
//=> http://domainxyz.com/dir1/dir2
于 2013-10-22T16:22:04.837 回答