I created a function that should check if a string correspond to an url format:
const test = (str) => {
const t = new RegExp(
'^(https?:\\/\\/)?' +
'(www\\.)' +
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|)' +
'(\\#[-a-z\\d_]*)?$',
'i',
);
return t.test(str);
};
console.log(test('http://demo.com')); //expect true
console.log(test('http://ww.demo.com')); //expect false
For each console.log() i wrote the expected value, in both cases i got false. In the last case false
is ok, but in the first i should get true
. How to fix the regex?