Remove the string on the beginning of an URL

JavascriptString

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

Yes, there is a RegExp but you don't need to use it or any "smart" function:

var url = "www.testwww.com";
var PREFIX = "www.";
if (url.startsWith(PREFIX)) {
  // PREFIX is exactly at the beginning
  url = url.slice(PREFIX.length);
}

Solution 3 - Javascript

If the string has always the same format, a simple substr() should suffice.

var newString = originalString.substr(4)

Solution 4 - Javascript

Either manually, like

var str = "www.test.com",
    rmv = "www.";

str = str.slice( str.indexOf( rmv ) + rmv.length );

or just use .replace():

str = str.replace( rmv, '' );

Solution 5 - Javascript

You can overload the String prototype with a removePrefix function:

String.prototype.removePrefix = function (prefix) {
    const hasPrefix = this.indexOf(prefix) === 0;
    return hasPrefix ? this.substr(prefix.length) : this.toString();
};

usage:

const domain = "www.test.com".removePrefix("www."); // test.com

Solution 6 - Javascript

Try the following

var original = 'www.test.com';
var stripped = original.substring(4);

Solution 7 - Javascript

const removePrefix = (value, prefix) =>
   value.startsWith(prefix) ? value.slice(prefix.length) : value;

Solution 8 - Javascript

You can cut the url and use response.sendredirect(new url), this will bring you to the same page with the new url

Solution 9 - Javascript

Another way:

Regex.Replace(urlString, "www.(.+)", "$1");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLupoView Question on Stackoverflow
Solution 1 - JavascriptnicosantangeloView Answer on Stackoverflow
Solution 2 - JavascriptberezovskyiView Answer on Stackoverflow
Solution 3 - JavascripttalnicolasView Answer on Stackoverflow
Solution 4 - JavascriptjAndyView Answer on Stackoverflow
Solution 5 - JavascriptFlavien VolkenView Answer on Stackoverflow
Solution 6 - JavascriptJaredParView Answer on Stackoverflow
Solution 7 - JavascriptArchibaldView Answer on Stackoverflow
Solution 8 - JavascriptOferView Answer on Stackoverflow
Solution 9 - JavascriptpirisView Answer on Stackoverflow