[javascript] Remove the string on the beginning of an URL

I want to remove the "www." part from the beginning of an URL string

For instance in these test cases:

e.g. www.test.com ? test.com
e.g. www.testwww.com ? testwww.com
e.g. testwww.com ? testwww.com (if it doesn't exist)

Do I need to use Regexp or is there a smart function?

This question is related to javascript string

The answer is


Depends on what you need, you have a couple of choices, you can do:

// this will replace the first occurrence of "www." and return "testwww.com"
"www.testwww.com".replace("www.", "");

// this will slice the first four characters and return "testwww.com"
"www.testwww.com".slice(4);

// this will replace the www. only if it is at the beginning
"www.testwww.com".replace(/^(www\.)/,"");

Similar questions with javascript tag:

Similar questions with string tag: