user1665355
user1665355

Reputation: 3393

Match parenthesis containing specific character

I have a string:

var string = "116 b (407) (90 A / 122 M) (11.2004)"

In that string I want to match specific parenthesis () and its context (i.e. (90 A / 122 M)) including specific character /.

I tried:

string.match(/\([/]\)/g)

But that returns null. What am I doing wrong? Best Regards

Upvotes: 2

Views: 63

Answers (3)

Shafizadeh
Shafizadeh

Reputation: 10340

Try this:

/\([^(/]*\/[^)]*\)/g

Online Pattern

Upvotes: 0

ibigbug
ibigbug

Reputation: 114

you can try string.match(/\([^)]*\)/g)

which may give what you want:

["(407)", "(90 A / 122 M)", "(11.2004)"]

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

Your \([/]\) pattern matches a (, followed with /, followed with ), a 3-char sequence.

You may use

/\([^()]*\/[^()]*\)/g

See this regex demo

Details:

  • \( - opening ( parenthesis
  • [^()]* - zero or more chars other than ( and ) (a \/ can be added at the end to improve performance a bit: [^()\/]*)
  • \/ - a / symbol
  • [^()]* - zero or more chars other than ( and )
  • \) - closing parenthesis

Upvotes: 4

Related Questions