Problem and Solutions
RegEx ignore word preciding a character set in node.js ?
By M.K. Verma · 2 May 2022

0
RexEx Groups:
result- one record from input (row)data- numbers (including,)cat- category nameextra- to be ignored
JS
- replace
resultwith re-orderedcat($3),=anddata($2) - replace
,withempty
const regex = /(?<result>(?<data>^[\d|,]+)(?: in )(?<cat>.+?)(?<extra>\s+(?:\(.+?\)?)?))$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<result>(?<data>^[\\d|,]+)(?: in )(?<cat>.+?)(?<extra>\\s+(?:\\(.+?\\)?)?))$', 'gm')
const str = `286,879 in Home & Kitchen (See Top 100 in Home & Kitchen)
339 in Cardboard Cutouts
2,945 in Jigsaws (Toys & Games)`;
const subst = `$3 = $2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst).replace(',', '');
console.log('Substitution result: ', result);