Javascript Regex matches the task of measuring length of inches
I am new to regex and I am trying to create a regex to match a string that contains a dimension expressed in feet and inches, with the option of fractional values โโin inches. So far I have the following expression:
var rex =/\s*(\d+)?\s*(?:'|ft|ft.|feet)?\s*-?\s*(\d+)?\s*(\d+)?\/?(\d+)?\s*(?:''|"|in|in.|inches)?/
To be as flexible as possible, I've made all captures optional to set dimensions such as 1ft
or 5''
as valid inputs.
var mstring = '5in';
var match = rex.exec(mstring);
The problem is that when applying a regex like to 5in
, the capture I get is equal match[1]='5'
, while the other three values โโ( match[2]
, match[3]
and match[4]
) remain undefined
.
Is there a way for the resulting values โโto be displayed in the order they are defined? In the above case, will be match[2]="5in"
, while match[1]
, match[3]
and match[4]
remain undefined
.
source to share
The trick is to use non-capturing groups around the optional parts and using the required subpatterns within those groups:
var rex =/(?:\s*(\d+)\s*(?:feet|ft\.|ft|'))?(?:\s*-\s*(\d+))?(?:\s*(\d+)\/)?(?:(\d+)\s*(?:inches|in\.|in|''|"))?/
In the first group I made it (?:feet|ft\.|ft|')
mandatory, in group 2 it is a hyphen -
, in group 3 it is a slash /
, and in group 4 it is alternation (?:inches|in\.|in|''|")
.
Watch the demo
EDIT:
Now I do not understand the logic, but if you want 5
to 5in
be displayed in the 2nd group, use
(?:\s*(\d+)\s*(?:feet|ft\.|ft|'))?(?:\s*-?\s*(\d+))?(?:\s*(\d+)\/)?(?:(\d+)\s*(?:inches|in\.|in|''|"))?
^
See Demo 2
source to share
I would consider doing some preprocessing - remove all spaces and do some conversion on the input. For example:
// normalize the input, e.g. 5 feet 11 inches becomes 5'11"
// any non-numerical character sequence starting with f is considering feet measurement, similar for inches (i.e. starting with i)
text = text.replace(/[^A-Z0-9'"]*/gi, '').replace(/f[^0-9]*/, "'").replace(/i[^0-9]*/, '"');
// regexp becomes simpler
var re = /(\d+')?(\d+")?/
source to share