John Cappelletti John Cappelletti - 5 months ago 7
jQuery Question

Looking for a regex to to Satisfy the following

I'm looking for a regex or logic in JavaScript to trap the following conditions

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.

Answer

Here's the regex you're looking for:

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

Then in JS, use it in the following way:

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

value of course is the string that you are testing.

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