具有特定国家/地区代码的电话号码的正则表达式

时间:2021-04-15 09:50:02

标签: typescript

我想为特定国家/地区代码和号码编写正则表达式

 const regexpwithCountryCode = new RegExp('^\\+(48)[0-9]{9}$');
       
 console.log( regexpwithCountryCode.test(String(+48124223232)) );

我基本上想验证上面的字符串是否正确,我的正则表达式应该接受以 + 开头的数字,然后是 48 位和 9 位数字。

我正在使用 https://regex101.com/ 进行测试,它说它是正确的,但是当我在带有 typecipt 的angularar 中使用它时,我得到控制台输出错误

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你必须把它变成这样的字符串:

const regexpwithCountryCode = /^\+48[0-9]{9}$/;

console.log(regexpwithCountryCode.test("+48124223232"));