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

Following is my text -Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de FinibusBonorum et Ma

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744921376a4601152.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信