0

I want to replace all the %(<>). String can be nested pattern as well.

para %(test) pattern %(te%(value)st) Hello Testing

I want it to be converted to

para MATCHFOUND pattern MATCHFOUND Hello Testing

Using the below regex

\%\((.*?)\)+

I get:

**para MATCHFOUND pattern MATCHFOUND*st)* Hello Testing**

Which is not correct as st) is not treated in pattern

Can anyone help me out with a proper regex to handle this.

Alen Genzić
  • 1,398
  • 10
  • 17
  • For nested regex patterns see https://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns – Cody G Jul 11 '18 at 14:12
  • Do you need balanced parenthesis? Also, is '2' the max amount of nesting? Or there's no limit? – Julio Jul 11 '18 at 18:17

1 Answers1

1

This one works for me: /\%\([\w\%\(\)]*\)/g


var string = "para %(test) pattern %(te%(value)st) Hello Testing"
var regex = /%\([\w%()]*\)/g
console.log(string.replace(regex, "MATCHFOUND"));
// "para MATCHFOUND pattern MATCHFOUND Hello Testing"
cornacchia
  • 473
  • 2
  • 9