1

I need to make the following extraction from string in JS:

'Sdfg dfg ldfgh (abc)' => ['abc']
'Sdfg dfg ldfgh (abc) ' => ['abc']
'Sdfg dfg ldfgh (abc) (cde)' => ['abc','cde']
'Sdfg dfg ldfgh (abc)(cde) (efgh)' => ['abc', 'cde', 'efgh']

I need to extract 'tags' in brackets, they may have spaces between them and also the whole string could have space in the end.

I've tried something like /(\(.*\))(\s?\(.*\))+/, but it's not enough to collect all the tags. How can I extract all I need having these optional spaces between and after tags?

shyamzzp
  • 115
  • 7
Dmitry Samoylov
  • 1,228
  • 3
  • 17
  • 27
  • What about`\(([^)]+)\)` you can find it here in this post: https://stackoverflow.com/questions/17779744/regular-expression-to-get-a-string-between-parentheses-in-javascript – GrimR1P Sep 02 '19 at 09:18

3 Answers3

1

The following regex should achieve what you require:

/(?<=\()\w*(?=\))/g) 

This pattern roughly translates to:

  1. (?<=() Look for a ( and start matching after that character
  2. \w* "Look for zero or more word characters and match those"
  3. (?=)) "Look for a ) and stop matching prior to this character"
  4. /g "Apply this pattern matching behavior globally across whole input string"

Using this pattern with the match() function will result in zero or more strings being returned in an array, where the contents of those strings correspond to the content between parantheiss pairs of your input string:

const pattern = /(?<=\()\w*(?=\))/g;

console.log('Sdfg dfg ldfgh (abc)'.match(pattern));
console.log('Sdfg dfg ldfgh (abc) '.match(pattern));
console.log('Sdfg dfg ldfgh (abc) (cde)'.match(pattern));
console.log('Sdfg dfg ldfgh (abc)(cde) (efgh)'.match(pattern));
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
0

You may match the consecutive parenthesized substrings and then split the result:

var s = 'Sdfg dfg ldfgh (abc)(cde) (efgh)';
var m = s.match(/\([^()]*\)(?:\s*\([^()]*\))*/) || [""];
console.log(m[0].replace(/^\(|\)$/g, '').split(/\)\s*\(/));

The \([^()]*\)(?:\s*\([^()]*\))* pattern will match:

  • \([^()]*\) - a (, 0+ chars other than ( and ) and then )
  • (?:\s*\([^()]*\))* - 0+ repetitions of
    • \s* - 0+ whitespaces
    • \([^()]*\) - same as above, (...) substring.

The .replace(/^\(|\)$/g, '') part will remove the first ( and last ), and .split(/\)\s*\(/) will split with )( having any amount of whitespaces in between.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Using String.prototype.match() with lookahead and lookbehind assertions:

const extract = (string) => string.match(/(?<=\().+?(?=\))/g);

console.log(extract('Sdfg dfg ldfgh (abc)'));
console.log(extract('Sdfg dfg ldfgh (abc) '));
console.log(extract('Sdfg dfg ldfgh (abc) (cde)'));
console.log(extract('Sdfg dfg ldfgh (abc)(cde) (efgh)'));
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156