Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find words to endpoint with regex javascript

Someone know how to I can do? I need to find the following words to a word until the end point.

Text example:

Product: My wonderful product.

I need: My wonderful product

I have managed to find the next word but not the others until the final point with this code:

const product= 'PRODUCT:';

let result = text.match(new RegExp(product+ '\\s(\\w+)', 'i'));

if (result != null) {
    result = result[1];
}

Thanks.

like image 652
OscarDev Avatar asked Dec 08 '25 19:12

OscarDev


1 Answers


// Input = Product: My wonderful product.
// Output = My wonderful product
const text = 'Product: My wonderful product.';
const product = 'PRODUCT: ';

const matches = text.match(new RegExp(`(?:${product})(.*)`, 'i'));

let result;
if (matches && matches.length === 2) {
  result = matches[1];
} else {
  result = '';
}

console.log(result)

See https://regex101.com/r/ZA4qHz/1/ to debug.

Relevant docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

like image 159
Joshua Robinson Avatar answered Dec 11 '25 10:12

Joshua Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!