最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Match the parentheses with or without a text in it - Regex - Stack Overflow

programmeradmin3浏览0评论

Following is my text -

Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).

In which I am trying to match the text -

  • (The Extremes of Good and Evil)
  • ()
  • ( )

My Regular Expression - \(.\) which is not working.

I also tried \(*\) which is matching (), ) of ( ) and ) of (The Extremes of Good and Evil). Let me know what I am doing wrong here.

Following is my text -

Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).

In which I am trying to match the text -

  • (The Extremes of Good and Evil)
  • ()
  • ( )

My Regular Expression - \(.\) which is not working.

I also tried \(*\) which is matching (), ) of ( ) and ) of (The Extremes of Good and Evil). Let me know what I am doing wrong here.

Share Improve this question asked Sep 24, 2017 at 19:13 NeshNesh 2,5819 gold badges40 silver badges56 bronze badges 4
  • Will your text have parentheses inside parenthesis? – lilezek Commented Sep 24, 2017 at 19:17
  • 2 What about (Foo(Bar))? – Ingo Bürk Commented Sep 24, 2017 at 19:18
  • try this \(.*\) – Eftakhar Commented Sep 24, 2017 at 19:19
  • @IngoBürk (?:()(.*|).*)(?:\b))|(?:()((?!)).*?)?(?:)) – Steve Kline Commented Sep 24, 2017 at 19:58
Add a ment  | 

3 Answers 3

Reset to default 5

You need a quantifier * to match zero or more characters inside the parenthesis. Also makes it lazy ? so it stops as long as it reaches the first close parenthesis \(.*?\):

var s = 'Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).'

console.log(
  s.match(/\(.*?\)/g)
)

My Regular Expression - \(.\) which is not working.

That matches a pair of parentheses with exactly one other character between.

I also tried \(*\) which is matching (), ) of ( ) and ) of (The Extremes of Good and Evil). Let me know what I am doing wrong here.

There, you're matching any number including zero of opening parentheses (because the wildcard applies to the opening parenthesis), followed by a closing parenthesis.

You want this:

\([^)]*\)

That is:

  • an opening parenthesis, followed by
  • zero or more characters other than a closing parenthesis, followed by
  • a closing parenthesis.

You need to exclude the closing parenthesis from the characters in the middle in some way, otherwise you'll match everything from the first opening parenthesis to the last closing one as a single match.

This should match exactly what you're looking for. When parsing using this on a non-global level for each line - it will parse off the parenthesis.

(?:\()  #Non-Capture Group Parenthesis - for advanced submatching regex.
(       # Start Capture Group 1
 (?!\)) # Negative Lookahead
   .*?  # Match all characters except line break + Lazy
)?      # End Capture Group 1 + Lazy (empty parenthesis)
(?:\))  #Non-Capture Group Parenthesis - for advanced submatching regex.

See below...

var s = 'Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).'

console.log(
  s.match(/(?:\()((?!\)).*?)?(?:\))/g)
)

//CONSOLE OUTPUT
(3) ["(The Extremes of Good and Evil)", "()", "( )"]
0: "(The Extremes of Good and Evil)"
1: "()"
2: "( )"
length: 3
发布评论

评论列表(0)

  1. 暂无评论