views:

506

answers:

5

Hi,

I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input:

5' 9"

6'

5'8"

Any ideas?

+1  A: 

Something like:

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

The second part refers to optional part of the heigth.

Remove the + if you want only one digit.

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

can also be used to detect that pattern within a text (avoid detecting 1234'45 as an heigth for... a person?!)

You can test that regexp here for javascript.

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

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

Andrew Hedges
+4  A: 

If you want to make sure that no one mucks around with it, you could limit it to sensible ranges, eg: 3' to 7'11''

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

I always thought that the "inches" mark was a double quote ("), compared to VonC's answer where he put it as two single quotes (''), so this regex takes both into consideration.

nickf
It is... " (Quote) for inches. Up-voted for sensible range functionality in your regex.
Richie_W
+1  A: 

Maybe something like:

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

see: here

Ruben
Nice copy/paste, but inappropriate here because we don't need 5 digits to represent the number of feet a *person* is tall.
Jan Goyvaerts
A: 

Ok. Thanks for all your input. Wow, that was fast, Big time.

Anyway, I've tested all your regex and it seems Ruben's answer passed all my test input. Thanks a lot for that mate.

So here's the one that I need:

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

Don't forget to mark his answer as the "answer" then :)
Timothy Khouri