score:3

Accepted answer

you may use

s.replace(/(@\[[^\][]*])\([^()]*?:\d+\)/g, '$1')

see the regex demo. details:

  • (@\[[^\][]*]) - capturing group 1: @[, 0 or more digits other than [ and ] as many as possible and then ]
  • \( - a ( char
  • [^()]*? - 0 or more (but as few as possible) chars other than ( and )
  • : - a colon
  • \d+ - 1+ digits
  • \) - a ) char.

the $1 in the replacement pattern refers to the value captured in group 1.

see the javascript demo:

const rx = /(@\[[^\][]*])\([^()]*?:\d+\)/g;
const remove_parens = (string, regex) => string.replace(regex, '$1');

let s = '@[admin](user:3) testing this string @[hellotessginal](user:4) hey!';
s = remove_parens(s, rx);
console.log(s);

score:0

try this:

var str = "@[admin](user:3) testing this string @[hellotessginal](user:4) hey!";
str = str.replace(/ *\([^)]*\) */g, ' ');
console.log(str);

score:0

you can replace matches of the following regular expression with empty strings.

str.replace(/(?<=\@\[(.*?)\])\(.*?:\d+\)/g, ' ');

regex demo

i've assumed the strings for which "admin" and "user" are placeholders in the example cannot contain the characters in the string "()[]". if that's not the case please leave a comment and i will adjust the regex.

i've kept the first capture group on the assumption that it is needed for some unstated purpose. if it's not needed, remove it:

(?<=\@\[.*?\])\(.*?:\d+\)

there is of course no point creating a capture group for a substring that is to be replaced with an empty string.

javascript's regex engine performs the following operations.

(?<=         : begin positive lookbehind
  \@\[       : match '@['
  (.*?)      : match 0+ chars, lazily, save to capture group 1
  \]         : match ']'
)            : end positive lookbehind
\(.*?:\d+\)  : match '(', 0+ chars, lazily, 1+ digits, ')'

Related Query

More Query from same tag