I have a string:
[](first-(second))
I would like to remove ( and ) between [](... and last ...)
With that being said, I'm currently using (?<=\[\]\()(.*?)(?=\))
I assume (.*?) needs to be replaced with some sort of expression that will find ( and ) in between.
Something similar to string.replace(/(?<=\[\]\()(.*?)(?=\))/g, '')
And the string should look like [](first-second)
CodePudding user response:
If you don't need a stylish code, why not to go the easy way? Extract the substring without external brackets, replace & concat. the removed chars
s = '[](first-(second))'
mySubstring = s.substring(3, s.length -2); // 'first-(second)'
//your replacement func.
result = '[](' mySubstring ')'; //'[](first-second)'
CodePudding user response:
Here is an example (not using lookaheads). It should steer you in the correct direction.
Using 2 capture groups, and some non-greedy sets, you can assemble your target string.
It assumes no additional nested parenthesis or brackets, although you can modify to your needs to only include alphanumeric characters in the character sets.
const d = '[](foo-(bar)) [](foo2-(bar2))'
const rx = /(\[\]\([^-\(\)\[\]]*?-)\(([^\)]*?)\)\)/g
const m = rx.exec(d)
console.log(m)
const fixed = d.replace(rx, '$1$2)')
console.log(fixed)
