2

I want to strip the following out of a string:

  • http://
  • https://
  • www.
  • www2.
  • /

An example string would be 'http://www.google.com', and I need to strip both 'http://' and 'www..'

I am trying to do this via Javascript's replace() using regex:

string.replace(/http:\/\/|www\.|www2\.|\//i,"");

The problem I'm encountering is that it will only strip one match from the regex. For example, in the above string only 'http://' would be removed.

Have I got the regex wrong or is replace only able to match once? I tested my expression on an online, non-JS regex builder and it worked fine.

Any help would be appreciated! Thanks!

1 Answer 1

4
//Add the /g tag at the end for "global"
string.replace(/http:(s?)|www(2?)|\//ig,"");
Sign up to request clarification or add additional context in comments.

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.