为什么这个正则表达式不起作用? [英] Why isnt this regex working?
本文介绍了为什么这个正则表达式不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为类似于以下地址的地址设置正则表达式模式,但我无法让测试方法返回 true,我在这里做错了什么?
I'm trying to have a regex pattern for an address similar to the one below but I cant get the test method to return true, what am I doing wrong here?
let reg=/[0-9]{3}\b[a-z]{1}\b\d{2}[a-z]{2}\b[a-z]{2}\b[a-z}{6}\b[a-z]{10}\b[0-9]{5}/;
let fakeAddress="925 s 10th st tacoma washington 98405";
reg.test(fakeAddress);
推荐答案
没有单词边界 字母和数字之间以及数字和字母之间,您想匹配它们之间的空格.请记住,\b
,一个字边界,是一个不消耗任何字符的零宽度断言.因此,您需要将所有 \b
替换为 \s+
(1 个或多个空白字符).另外, [a-z}
是一个错字.应该是 [a-z]
.
There are no word boundaries between letters and digits and between digits and letters, you wanted to match whitespace between them. Remember that \b
, a word boundary, is a zero-width assertion that does not consume any chars. So, you need to replace all \b
with \s+
(1 or more whitespace chars). Also, [a-z}
is a typo. it should be [a-z]
.
使用
/\d{3}\s+[a-z]\s+\d{2}[a-z]{2}\s+[a-z]{2}\s+[a-z]{6}\s+[a-z]{10}\s+[0-9]{5}/
查看正则表达式演示
这篇关于为什么这个正则表达式不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文