17

How to replace the host part of a URL using javascript regex. This can be any kind of URL with or without http. Assume this text is from the content of a json file.

OldText:

{
   "auth" : {
     "login" : "http://local.example.com:85/auth/signin",
     "resetpass" : "http://local.example.com:85/auth/resetpass",
     "profile" : "http://local.example.com/auth/profile"
   }
}

Expecting a solution like:

var NewText = OldText.replace (/(some regex)/g, 'example.com');

To get NewText as:

{
  "auth" : {
     "login" : "http://example.com:85/auth/signin",
     "resetpass" : "http://example.com:85/auth/resetpass",
     "profile" : "http://example.com/auth/profile"
    }
}

I found the same here, but that regex won't work in javascript.

Note: I'm looking for the Regex.

1
  • Do you specifically need to use regex? If you need to update the url from javascript you could potentially re-assign window.location.host. Reference Commented Jan 11, 2017 at 4:06

4 Answers 4

52

You can use the URL function and set a new hostname:

var oldUrl = "http://host1.dev.local:8000/one/two";
var url = new URL(oldUrl);
url.hostname = 'example.com';
url.href //'http://example.com:8080/one/two'
Sign up to request clarification or add additional context in comments.

2 Comments

This is not supported in Edge, IE 11 and Safari
@E.Fortes Yes, it is! Just tested on Edge right now: var test = new URL("google.com/path?search=foo"); test.hostname = "localhost"; test.port= "7071"; alert(test.href);
1

This could be achieved easily using:

var NewText = OldText.replace (/(https?:\/\/)(.*?)(:*)/g, '$1' + 'example.com' + '$3'); 

You are welcome to modify this with the best practice.

1 Comment

I think the correct regex pattern should be: /(https?:\/\/)(.*?)(\/.*)/g
0

I think this is completely doable using regex. Here's a small snippet for you, let me know if you want to add something else to it.

var urls = [
    {
      url: "http://local.something.com:85/auth/signin"
    }, 

    {
      url: "local.anything.com/auth/profile"
    }
];
for(var i in urls) {
  var newUrl = urls[i].url.replace(/(http:|)(^|\/\/)(.*?\/)/g, 'https://example.com/');
  console.log(newUrl);
}

I am assuming, from

without http

, you mean something like 'google.com'

Comments

0

I found the above didn't work when I wanted to change the port as well, so I cobbled this together for my use case

var hostname = 'some.domain.name:6101';
var oldUrl = 'https://localhost:7443/my/url/test?easy=5';
var url = new URL(oldUrl);
// helpful for debugging it
// console.log(url);
var newUrl = url.protocol + '//' + hostname + url.pathname + url.search;
console.log(newUrl);

https://some.domain.name:6101/my/url/test?easy=5

Or as suggested in the comments by @Barmar

var oldUrl = "http://host1.dev.local:8000/one/two?test=5";
var url = new URL(oldUrl);
url.hostname = 'example.com';
url.port = '6101';
url.href // http://example.com:6101/one/two?test=5

Or, as I finally ended up using for my use case, I just switched the relatie url, and it knew to leave the domain/port/protocol alone, so it worked on multiple different versions of the same site, hosted at different domains

window.location.href = '/one/two?test=5'; // http://example.com:6101/one/two?test=5

2 Comments

Why not url.port = 6101?
Yeah thanks, that totally works and is simpler overall - I will list that a well :D !

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.