I am attempting to remove only the prefix www.
from a URL using JavaScript. Initially, I attempted to utilize the regular expression /^(?:www\.)?/i
:
"www.example.com".replace(/^(?:www\.)?/i, "")
This method worked successfully for URLs without a protocol (e.g., www.example.com). However, it failed when trying to remove the prefix from URLs with protocols attached, such as https://www.example.com
.
My goal is to achieve the following transformations:
https://www.example.com/ -> https://example.com/
WWW.example.com/ -> example.com/