user8euryhgf
user8euryhgf

Reputation: 13

Why isnt this regex working?

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);

Upvotes: 1

Views: 44

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626709

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].

Use

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

See the regex demo

Upvotes: 1

Related Questions