alexa

RegEx ignore word preciding a character set in node.js ?

RegEx ignore word preciding a character set in node.js ?

0

 

RexEx Groups:

  1. result - one record from input (row)
  2. data - numbers (including ,)
  3. cat - category name
  4. extra - to be ignored

JS

  • replace result with re-ordered cat ($3), = and data ($2)
  • replace , 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);

227 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry