Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text between two rounded brackets

How can I retrieve the word my from between the two rounded brackets in the following sentence using a regex in JavaScript?

"This is (my) simple text"

like image 430
Vikas Avatar asked Aug 21 '12 16:08

Vikas


People also ask

What are the fancy parentheses called?

Curly brackets { and } are also known as "curly braces" or simply "braces" (UK and US), "definite brackets", "swirly brackets", "birdie brackets", "French brackets", "Scottish brackets", "squirrelly brackets", "gullwings", "seagulls", "squiggly brackets", "twirly brackets", "Tuborg brackets" (DK), "accolades" (NL), " ...

What are parentheses regex?

Parentheses Create Numbered Capturing Groups Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses. The regex Set(Value)? matches Set or SetValue.


2 Answers

console.log(    "This is (my) simple text".match(/\(([^)]+)\)/)[1]  );

\( being opening brace, ( — start of subexpression, [^)]+ — anything but closing parenthesis one or more times (you may want to replace + with *), ) — end of subexpression, \) — closing brace. The match() returns an array ["(my)","my"] from which the second element is extracted.

like image 122
Michael Krelin - hacker Avatar answered Oct 06 '22 01:10

Michael Krelin - hacker


var txt = "This is (my) simple text"; re = /\((.*)\)/; console.log(txt.match(re)[1]);​ 

jsFiddle example

like image 33
j08691 Avatar answered Oct 06 '22 00:10

j08691