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"
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.
var txt = "This is (my) simple text";
re = /\((.*)\)/;
console.log(txt.match(re)[1]);
jsFiddle example