Skip to content

Instantly share code, notes, and snippets.

@manuganji
Last active November 25, 2019 18:12
Show Gist options
  • Save manuganji/6053226 to your computer and use it in GitHub Desktop.
Save manuganji/6053226 to your computer and use it in GitHub Desktop.
JavaScript Regex to figure out the value in inches when the input value is in feet-inch notation( Eg: height) . Properly catches 5'10", 11'10", 5'2", 5'07, 5', 5'", 5 Also fails correctly for 5'13", 5'233", 521'1",
height_val = $("#id_height").val();
var regex_op = /^(\d{1,2})[\']?((\d)|([0-1][0-2]))?[\"]?$/g.exec(height_val);
//very rare chance for someone to be of two digit feet height.
//but i put it there, just in case.
var feet = regex_op[1];
var inches = regex_op[2];
// converting to inches
var height = (parseInt(feet) || 0) * 12 + (parseInt(inches) || 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment