I have a requirement where in a user input the regular expression should match only numbers either digits or numbers with decimal points, however if there is a number followed with a percentage symbol then that should be excluded, but numbers followed with alphabets like 100g or 500 grams should be matched without picking up the alphabet part
I have come up with the following:
let numberPart = importValue.match(/\d (?:[\.,]\d )?\b(?!(?:[\.,]\d )|(?:\s?%|\spercent))/g);
11 grams nutella 1,5% is matched and we get 11 as output
however 11grams nutella 1,5% isn't matched
how can I change it so it matches numbers followed immediately by an alphabet or alphabets.
CodePudding user response:
I think this is what you're looking for
/([\d.,] (?!%))/g
EDIT: I have edited my answer, it now includes commas and dots.
CodePudding user response:
You have a boundary \b inside your regex. Removing it solves the issue:
let numberPart = importValue.match(/\d (?:[\.,]\d )?(?!(?:[\.,]\d )|(?:\s?%|\spercent))/g);
You can debug your regex at regex101.com.
