Lukas Lukas - 1 year ago 35
Javascript Question

Remove the string on the beginning of an URL

I want to remove the "

" part from the beginning of an URL string

For instance in these test cases:



(if it doesn't exist)

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

Answer Source

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

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

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

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