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

8

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I suggest replacing i.substr(1, i.length-2) with i.slice(1,-1)
@JanStránský yeah!
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

Comments

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);

1 Comment

list.map(item => item.slice(1,-1)); is maybe more readable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.