2

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

coinhndp
  • 2,281
  • 9
  • 33
  • 64
  • 3
    Have you tried anything at all? Where are you stuck? – Felix Kling Sep 21 '20 at 13:16
  • I tried this but not work: '(hello) world this is (hi) text'.match('\((.*?)\)') –  coinhndp Sep 21 '20 at 13:20
  • 1
    @coinhndp Please edit your question and put that information in it – Jan Stránský Sep 21 '20 at 13:22
  • `match` only returns the first match if you don't use the `g` flag for the regular expression: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match . Also you are passing a string literal, not a regular expression which makes things trickier. You can learn about regular expressions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions – Felix Kling Sep 21 '20 at 13:23
  • Which makes it trickier? Could you explain it for me –  coinhndp Sep 21 '20 at 13:26
  • brackets are a meta-character (in most regex flavours at least), you need to escape them when you want to match the actual characters. I'd use `\(([^)]*)\)`, but `\((.*?)\)` is fine too – Aaron Sep 21 '20 at 13:26
  • If you use a string literal instead of an regular expression literal you have to "double escape" meta characters because the backslash is also the escape character in string literals. It's not difficult, but it trips people up who don't understand how escape sequences work. – Felix Kling Sep 21 '20 at 13:47
  • 1
    Possible duplicate of [Regular Expression to find a string included between two characters while EXCLUDING the delimiters](https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclud) – Jan Stránský Sep 21 '20 at 14:38

3 Answers3

2

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

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
1
'(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);
Jan Stránský
  • 1,671
  • 1
  • 11
  • 15
n1md7
  • 2,854
  • 1
  • 12
  • 27
1

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
Jan Stránský
  • 1,671
  • 1
  • 11
  • 15