Remove the string on the beginning of an URL Ask Question

Remove the string on the beginning of an URL Ask Question

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

For instance in these test cases:

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

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

ベストアンサー1

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\.)/,"");

おすすめ記事