![](/img/trans.png)
[英]How to write the regex which detects 9 digit number only and should not consider if it has a X or x preceded to that number and mask first 5 digit?
[英]I need a regex for a telephone number which does not accept 0 as the first digit of the 3 digit US area code
我目前正在使用以下正则表达式来匹配电话号码
'\\([1-9]{3}\\)\\s{1}[0-9]{3}-[0-9]{4}'
但是上述模式不允许前3位数字为0,并且当我将其修改为
'\\([0-9]{3}\\)\\s{1}[0-9]{3}-[0-9]{4}'
它接受0作为第一位数字。 我想生成一个正则表达式,它的第一个数字不接受0,但其余的数字却接受。
我已经修改了我认为适合我需要的正则表达式,但是我不确定(从未使用过正则表达式模式)并且不知道如何在regex101上对其进行测试
'\\([1-9]{1}[0-9]{2}\\)\\s{1}[0-9]{3}-[0-9]{4}'
如果有人可以帮助我,就像您可以指出我是否朝着正确的方向前进,那将是惊人的
我正在寻找这个问题的反函数,答案是确保数字以0开头,但是我正在寻找以下实现的反函数
谢谢你,维杰
尝试这个:
/\([1-9]\d\d\)\s\d{3}-\d{4}/;
要么:
new RegExp('\\([1-9]\\d\\d\\)\\s\\d{3}-\\d{4}');
说明:
\( : open paren
[1-9] : a digit (not 0)
\d\d : 2 digits (including 0)
\) : close paren
\s : one space
\d{3} : 3 digits (including 0)
- : hyphen
\d{4} : 4 digits (including 0)
这应该工作。
正则表达式:
[1-9]\d{2}\-\d{3}\-\d{4}
输入:
208-123-4567
099-123-4567
280-123-4567
输出:
208-123-4567
280-123-4567
JavaScript代码:
const regex = /[1-9]\\d{2}\\-\\d{3}\\-\\d{4}/gm; const str = `208-123-4567 099-123-4567 280-123-4567`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.