How to remove string-specific word?

3

I have an input where the user can insert his own site, and I insert http: // when writing to db, but I would like to treat it so that if there is already this tag in the input it does not insert again, since the user updates he re-inserted the tag and gets link

    
asked by anonymous 09.06.2017 / 14:44

3 answers

2

You can use indexOf :

let uri = 'meusite.com'

// -1 é não encontrado
if (uri.indexOf('http://') == -1 && uri.indexOf('https://') == -1){
   uri = 'http://' + uri;
}
    
09.06.2017 / 14:48
4

You can do this:

let url = "https://google.com";

let novaUrl = url.replace(/^https?:\/\//, '');

console.log(novaUrl);
    
09.06.2017 / 14:47
4

A simple replace works.

You could also use a regex in replace, to remove both "http: //" and "https: //".

let input = 'http://pt.stackoverflow.com';
input = input.replace('http://', '');

console.log(input);

let input2 = 'https://pt.stackoverflow.com';
input2 = input2.replace(/^https?:\/\//,'', '');

console.log(input2);
    
09.06.2017 / 14:49