zwitterion zwitterion - 4 months ago 8
Javascript Question

REGEX pattern for javascript

I have this pattern to be tested in javascript using REGEX:

nn.nnn.nnn./nnnn-nn


where n can be any integer between 0-9.

And I have this regex that works.

[0-9]{2}[.][0-9]{3}[.][0-9]{3}\/[0-9]{4}[-][0-9]{4}


Is there another more elegant way to rewrite this expression to grab the same pattern?

Answer

There's a few possible simplifications:

[0-9] -> \d
[.] -> \.
[-] -> -
nnn.nnn. -> (\d{3}\.){2}

nn.nnn.nnn./nnnn-nn -> \d{2}\.(\d{3}\.){2}\/\d{4}-\d{2}

Your pattern asks for 4 digits at the end, but not your data sample. I followed the sample.