我正在寻找 JavaScript 中的正则表达式或逻辑来捕获以下条件
If a value begins with a number [space] Street call X
5 Eastern
500 Eastern
25 15th
..
If NO street number call Y
Eastern
15th St
N Eastern
任何帮助或指导将不胜感激。
请您参考如下方法:
这是您正在寻找的正则表达式:
/^\d+\s[A-Z0-9][a-z]+/
然后在JS中,按如下方式使用:
if (/^\d+\s[A-Z0-9]+[a-z]+/.test(value)) {
x();
}
else {
y();
}
value
当然是您正在测试的字符串。
测试...
/^\d+\s[A-Z0-9]+[a-z]+/.test('5 Eastern')
// => true
/^\d+\s[A-Z0-9]+[a-z]+/.test('500 Eastern')
// => true
/^\d+\s[A-Z0-9]+[a-z]+/.test('25 15th')
// => true
/^\d+\s[A-Z0-9]+[a-z]+/.test('Eastern')
// => false
/^\d+\s[A-Z0-9]+[a-z]+/.test('15th St')
// => false
/^\d+\s[A-Z0-9]+[a-z]+/.test('N Eastern')
// => false