IgorAlves
IgorAlves

Reputation: 5540

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?

Upvotes: 0

Views: 375

Answers (2)

plalx
plalx

Reputation: 43718

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.

Upvotes: 1

DRC
DRC

Reputation: 5048

Read my comment, and probably you should correct it to something like:

\d{2}\.\d{3}\.\d{3}\.\/\d{4}-\d{2}

Upvotes: 0

Related Questions