简体   繁体   English

寻找一个正则表达式来满足以下要求

[英]Looking for a regex to to Satisfy the following

I'm looking for a regex or logic in JavaScript to trap the following conditions 我正在寻找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

Any help or direction would be appreciated. 任何帮助或指示,将不胜感激。

Here's the regex you're looking for: 这是您要查找的正则表达式:

/^\d+\s[A-Z0-9][a-z]+/

Then in JS, use it in the following way: 然后在JS中,通过以下方式使用它:

if (/^\d+\s[A-Z0-9]+[a-z]+/.test(value)) {
  x();
}
else {
  y();
}

value of course is the string that you are testing. value当然是您要测试的字符串。

Tests... 测试...

/^\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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM