Regular expression for content between and including parent

Can someone help me with a regex that will catch the following:

  • should be at the end of the line
  • remove all characters between (and) including parentheses

This will be done in javascript.

that's what I still have -

var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, "");

It seems to catch some characters outside of the parent codes.

+5
source share
3 answers

This refers to the coincidence between the parens and the only line: \([^(]*\)\s*$. If paranas can be nested, you need a parser, not a regular expression.

+1
source
var title = $(this).find('title').text().replace(/\([^()]*\)\s*$/, "");

must work.

< >, , , , mystr.replace(/[<>]+/g, "");

(, , ( ) ), .

( ). , \s* .

0

Where $? You need a dollar at the end and maybe catch extra gaps.

var title = $(this).find('title').text().replace(/\s*\([^\)]*?\)\s*$/, "");

If the brackets can also be angle brackets, then this can also be the same:

var title = $(this).find('title').text().replace(/\s*(\([^\)]*?\)|\<[^\>]*?\>)\s*$/, "");
0
source

All Articles