As a junior backend developer, my experience with JavaScript is limited. I am attempting to standardize the format of stored URLs as shown below:
www.hello.com
hello.com
http://hello.com
https://hello.com
Currently, if I input hello.com
, it automatically converts to http://www.hello.com
, which is great!
However, I am struggling to modify this function to also convert www.hello.com
to http://www.hello.com
.
My issue now is that it results in http://www.www.hello.com
. There must be a way to remove the extra www or adjust the function slightly?
function checkURL(abc){
var string = abc.value
if(!(/^http:\/\//.test(string))){
string = "http://www." + string;
}
abc.value=string
}
edit
The main problem I'm facing is on the backend (as a Python/Django developer) where I use the URL as part of a POST request in a user form. I aim to store all URLs in a consistent format for easy comparison and matching purposes. Therefore, whether a URL is entered as hello.com
, www.hello.com
, or even http://hello.com
, I want them all saved in the same format for reliable company matching when querying the database. I believe using a JavaScript function is the most suitable solution here. While I don't necessarily need the www
prefix, consistency in formatting is crucial.