const regex = /.*(?:\d(?:\.\d+)?\s*x\s*)?\K(?<!\d)\d+(?:\.\d+)?(?:k?g|m?l)\b|(?<!\d)\d+(?:\.\d+)?(?:k?g|m?l)(?=\s*x\s*\d)/gi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('.*(?:\\d(?:\\.\\d+)?\\s*x\\s*)?\\K(?<!\\d)\\d+(?:\\.\\d+)?(?:k?g|m?l)\\b|(?<!\\d)\\d+(?:\\.\\d+)?(?:k?g|m?l)(?=\\s*x\\s*\\d)', 'gi')
const str = `product brand A 1237ml Bundle of 6
product milk choc370ML
brand milk Vanilla Flavor 850g
One 2400g, For 0-6 Month-Old Infants
a+...two...6-12months...11.2kg...milk
a+...two...11.2kg 6-12months ..milk
Product 200g (10x2g)
[200g] Product
Product A brand(300g)
Product 200g (20g x 10)`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions