开发者

regex for content between and including the parenthees

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

  • has to be at the end of the string
  • remove all characters between ( and ) including the parentheses

It's going to me done in javascript.

here's what i have so far -

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

It seems to be开发者_运维技巧 catching some chars outside of the parenthees though.


This deals with matching between parens, and only at the of the string: \([^(]*\)\s*$. If the parens might be nested, you need a parser, not a regular expression.


Where's the $? You need a dollar at the end and possibly catch optional whitespace.

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

If brackets can also be angle brackets, then this can match those too:

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


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

should work.

To remove < and > you don't really need regexes, but of course you can do a mystr.replace(/[<>]+/g, "");

This will match a (, any number of characters except parentheses (thereby ensuring that only the last parentheses will match) and a ), and then the end of the string.

Currently, it allows whitespace between the parentheses and the end of the string (and will remove it, too). If that's not desired, remove the \s* bit from the regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜