Javascript regex to match person's height

I need a javascript regex pattern to match the height of the person in order to validate the input. Here are some examples of input:

5 '9 "

6 '

5'8 "

Any ideas?

+1


source to share


5 answers


If you want to make sure no one is cheating with it, you can limit it to reasonable ranges, for example: 3 'to 7'11' '

/^(3-7)'(?:\s*(?:1[01]|0-9)(''|"))?$/

      



I always assumed that the "inches" mark was a double quote ( "

) compared to VonC's answer where he put this as two single quotes ( ''

), so this regex counts both values.

+6


source


Maybe something like:

^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$ 

      



see: here

+2


source


Something like:

\d'(?:\s*\d+'')?

      

The second part refers to the optional part of the vertex.

Remove the + if you only want one digit.

\b\d'(?:\s*\d+'')?\b

      

can also be used to detect this pattern in text (avoid detecting 1234'45 as a top for ... face ?!)

You can check this regexp here for javascript .

+1


source


Ok. Thanks for all your answers. Wow, that was fast. Great time.

Anyway, I've checked all of your regexes, and it seems that Ruben's answer went through all of my test input. Thanks a lot for this helper.

So, here's what I need:

^ (\ d {1,5}) \ '((\ s) (-) (\ S) ([0-9] | ?? (1 [0-1])) \ ")? $

+1


source


^\d'\s?(\d{1,2}")?$

      

Tested here: http://www.regular-expressions.info/javascriptexample.html

0


source







All Articles