Get text between two rounded brackets

JavascriptRegex

Javascript Problem Overview


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"

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

jsFiddle example

Solution 3 - Javascript

You may also try a non-regex method (of course if there are multiple such brackets, it will eventually need looping, or regex)

init = txt.indexOf('(');
fin = txt.indexOf(')');
console.log(txt.substr(init+1,fin-init-1))

Solution 4 - Javascript

For anyone looking to return multiple texts in multiple brackets

var testString = "(Charles) de (Gaulle), (Paris) [CDG]"
var reBrackets = /\((.*?)\)/g;
var listOfText = [];
var found;
while(found = reBrackets.exec(testString)) {
  listOfText.push(found[1]);
};

Solution 5 - Javascript

Use this to get text between closest ( and ):

const string = "This is (my) (simple (text)"
console.log( string.match(/\(([^()]*)\)/)[1] )
console.log( string.match(/\(([^()]*)\)/g).map(function($0) { return $0.substring(1,$0.length-1) }) )

Results: my and ["my","text"].

EXPLANATION

--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^()]*                   any character except: '(', ')' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \)                       ')'

Solution 6 - Javascript

to get fields from string formula.

var txt = "{{abctext/lsi)}} = {{abctext/lsi}} + {{adddddd}} / {{ttttttt}}";
re = /\{{(.*?)\}}/g;
console.log(txt.match(re));

Solution 7 - Javascript

to return multiple items within rounded brackets

 var res2=str.split(/(|)/);

  var items=res2.filter((ele,i)=>{
  if(i%2!==0) {
  return ele;
  }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVikasView Question on Stackoverflow
Solution 1 - JavascriptMichael Krelin - hackerView Answer on Stackoverflow
Solution 2 - Javascriptj08691View Answer on Stackoverflow
Solution 3 - JavascriptSexyBeastView Answer on Stackoverflow
Solution 4 - JavascriptYu Mai LinView Answer on Stackoverflow
Solution 5 - JavascriptRyszard CzechView Answer on Stackoverflow
Solution 6 - JavascriptUsman ShahView Answer on Stackoverflow
Solution 7 - JavascriptRonView Answer on Stackoverflow