Regexp - ned to cut value after some word in upper case or lower case

I need to find word in the text. It can be "product code" or 'Product code', find all entries and cut the value after the space. It can include numbers, letters, dots, slashes. The value can be several charters. But it can end with comma or space. I need to cut this value. That is my solution:

const body = document.body.innerText;
let regExp3 = /product code [0-9]*.[0-9]*/* $,/ig;
let re = let re = /product code/gi;
let qw = [...body.match(regExp3)];
let str = qw.join(' ');
const strre = str.replace(re, '');
console.log(strre);

But it's not working.


Ответы (2 шт):

Автор решения: Andrei

Try this

 let text = `
    product code 123.456/Abc, ends with comma
    Product code Abc.Def/123 and more text after the space
    `;

    let text2 = text.replace(/(product code) [\w\/.]+/gi, '$1');
    console.log(text2);

Обяснение

  • (product code) - группа (другими словами текст), который будет вставляться вместо всего соответствия
  • [\w\/.]+ - непрерывная последовательность символов, содержащая буквы, цифры, слеш (/), точку(.) в количестве от 1 символа до бесконечности
  • /gi - директивы обозначающие поиск всех соответствий (g) без учета регистра текста (i)
  • $1 - ссылка на 1 группу
→ Ссылка
Автор решения: user2240578

Use This OneLiner:

> [...'product code 228-WASD'.matchAll(/(?<=product code )\S+/ig)].map(a => a[0])
['228-WASD']
→ Ссылка