Regex get string between ()

Viewed 86

I have the text

   var text =  (hello) world this is (hi) text

I want to write a regex function so I can get

parseText(text) // returns ['hello', 'hi']

I tried this but not work:

'(hello) world this is (hi) text'.match('((.*?))')

Thanks for your help

3 Answers

you can try with: /\([^\)]+\)/g

  1. \(: escaped char
  2. [^\)]+: one or more character(including symbols) until ) char.
  3. \): escaped char
  4. g flag: search all coincidences

enter image description here

const regex = /\([^\)]+\)/g;
const str = `(hello) world this is (hi) text`;

console.log(
  str.match(regex) // this returns an string array
    .map(i => i.slice(1, -1)) // remove first and last char
);

TIPS:

About point #2, you can change to [\)]* to take effect over zero or more character.

If you need only string, you can use \w+ or \w*.

If you need only words you can use /\(\b\w+\b\)/g

'(hello) world this is (hi) text'.match(/\([\w]*\)/g)

This returns [ "(hello)", "(hi)" ] and you can run another parse function to remove that extra parenthesis.

const text = '(hello) world this is (hi) text';
const list = text.match(/\([\w]*\)/g);
const parsed = list.map(item => item.replace(/\(|\)/g, ''));
console.log(parsed);

You can find several options in this post.

Apart from using groups or postprocessing of the match results, you can use single regex match using lookahead / lookbehind:

var text = " (hello) world this is (hi) text"
var output = text.match(/(?<=\().*?(?=\))/g)
console.log(output)

output:

[ 'hello', 'hi' ]

Explanation:

  • (?<=...) ... positive lookbehind. The match is preceded be ..., but the ... is not included in the match
  • (?<=\() ... positive lookbehind for ( character
  • .* ... zero or more times of any character
  • .*? ... nongreedy version of .*
  • (?=...) ... positive lookahead, the match is followed by ... but the ... is not included in the match
  • (?=\)) ... positive lookahead for ) character
  • /.../g ... g is global flag, match finds all, not only the first, occurrence
  • do not forget to escape "special characters", e.g. parentheses
Related