Regex Not Functioning as Expected


Hello,

According to my regex checker, I have successfully created a sequence that matches several types of UK telephone numbers, however when I try to put this into effect in my code I get a return of ‘false’.

Please advise :slight_smile:

const tel = "+44 7868 193 061";

const validMobileNumber = num => num == /^(\+44\W?7\d{3}|07\d{3}|0044\W?7\d{3})\W?\d{3}\W?\d{3}$/g;

console.log(validMobileNumber(tel));

Ah, so in my regex lessons I was never informed about ‘.test’ method.

This seems to have solved the problem:

const validMobileNumber = num => /^(\+44\W?7\d{3}|07\d{3}|0044\W?7\d{3})\W?\d{3}\W?\d{3}$/g.test(num);
1 Like

can you click ‘solved’ on your own posts?
for anyone new to regex i did a more step by-step thing (but this is NOT a solution to any fcc challenge)

let tel = "+44 7868-193061"

function validMobileNumber(num){
  num=num.replace(/\s/g,"")
  num=num.replace(/\-/g,"")
  num=num.replace(/^\+44/g,"0")
  num=num.replace(/^(0044)/g,"0")
  console.log('tel:',tel)
  return /^07\d{9}$/.test(num)

}

console.log(validMobileNumber(tel))
1 Like

Yes you can, but don’t put the solution up in full…

1 Like