const regex = /(?<!\d|-|\/|to|\$)(?<!\$(\d)\.)([1-9][0-9]{0,2}(\.\d{0,2})?\s?)(lbs|lb|LBS|LB|KG|kg|G|g|L|l+)(?!\d|\w)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<!\\d|-|\\\/|to|\\$)(?<!\\$(\\d)\\.)([1-9][0-9]{0,2}(\\.\\d{0,2})?\\s?)(lbs|lb|LBS|LB|KG|kg|G|g|L|l+)(?!\\d|\\w)', 'gm')
const str = `10lb Potato bag
Apples Gld Delicious 3lb
Grandfer Rocha Pear2.5lbs
CHIN.MAND.5LB
Batata Veg (10LB)
SIG 170LB99 Legging SzB 1ea
ARJO CDA1450035 SupBlk 400LB1ea
AHC EB205L
DRI 10220-1 Bariatric500lb 1 ea
TILDA SONA MASOORI 20 LBS
OVAL ROASTER 9 - 12 LBS
ORGANIC TRIPACK FRUIT 1.2 KG
KGF5KT2404 PEPLUM T, PURPLE
AIR 1773LB-XL 15-20 KnXL 1ea
PLUM BAG 1KG US
NEXT XTRA 100G
SUDOCREM 60G
EGGPLANT GRILLED IN OIL (3KG)
CORONATION GRAPE 2L CA
MEAT DEAL \$11.00 KG
LIVE LOBSTER- 2.00-3.00 LB-MSC
PC MAGIC GROW 20-20-20 3.55KG
KGF7W3409 KGVELCROWB,GREY
TZATZIKI 500G
2+1 LEAD MIX`;
// 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