I’m using javascript to parse through some strings.
I have some strings that look like this:
1234 1/2 Old Town Alexandria
I have written a regular expression to identify instances in which a fraction comes after the street number:
var addressFractionRegex = /^\d*\s+(1[/]\d)\s+/i;
What I would like to do is remove the fraction part of the regular expression from the source string only in cases where the entire regular expression is matched (ie: the fraction is preceded by a street number). In this case, I would end up with (removing the “1/2”):
1234 Old Town Alexandria
I would also like to save the part that I removed into another variable. So ideally, I’d end up with something like:
var street_address = '1234 Old Town Alexandria';
var fraction = '1/2';
How is this done in javascript?
Thanks!
replace can take a function:
Demo