0
RexEx Groups:
result
- one record from input (row)data
- numbers (including ,
)cat
- category nameextra
- to be ignoredJS
result
with re-ordered cat
($3
), =
and data
($2
),
with empty
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);
* Be the first to Make Comment