Reputation: 165
I'm writing an integer parser in Javascript that takes in verbal descriptions of numbers up to a million. At the moment, I need to get rid of (space)thousand
in a string like two hundred forty-two thousand
, most preferably using a capture group containing the two hundred forty-two
. My problem is, the thousand
may or may not be there, and using ?
in this case stops my regex from working as intended. Here's what I've got so far:
Considering the string two hundred forty-two thousand
(.* ?)( thousand)
first capture group contains two hundred forty-two
, but it doesn't work if the string doesn't end with thousand
(.* ?)( thousand)?
works without the thousand
in the string, but first capture group yields the entire string at all timesWhat I need is an expression that puts the whole string in a capture group, except the last, specified word whether it exists or not in the string.
I've been searching a lot for a solution to this, but I couldn't find anywhere a case in which the excluded word is optional in the string
Upvotes: 1
Views: 65
Reputation: 5963
var re = /^(.*?)( thousand)?$/;
console.log("two hundred forty-two thousand".match(re));
console.log("two hundred forty-two".match(re));
.*?
to match lazily (i.e. non-greedily) up to an optional thousand
.Upvotes: 1
Reputation: 30985
You can create a non capturing group to match the thousand
pattern and discard it if present, if it is not there the alternation will match until the end of line.
I can come up with this regex:
(.+?)(?: thousand|$)
Upvotes: 1