0

I am trying to see how to use reg expressions to replace a url up to the .com, with something like local host. I am not a guru with expressions, so having a lot of troubles doing this. It is so we can have a Chrome extension for our localhost, and swap all urls to point to local host for debugging. So the expression needs to find anything like the following:

*.com/ --> and beyond stays the same, but up to the .com is swapped with our temp string value.

Thanks in advance

1
  • url = url.replace(/^.+?\.com\b/i, 'http://localhost'); Commented Oct 5, 2015 at 18:55

2 Answers 2

1

if you want to use chrome extension then * is valid

    chrome.webRequest.onBeforeRequest.addListener(
            function(details) {
              details.url = "http://localhost";
            },
            {urls: ["*://*.*.com/*"]}, //could also be urls: ["*://my.host.com/*"]
//also if you want to change all urls you can use ["<all_urls>"]
            ["blocking"]);
Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to accomplish it with this if you want to go the regex route:

.*?(?=\.com\b)

https://regex101.com/r/aA5rT3/1

It uses a positive lookahead to match up until the .com.

here is the fiddle: https://jsfiddle.net/520zdnsL/

Comments

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.