-4

I have following regular Expression: /prefix(([^)]+))/g

This expression matches everything between 'prefix(' and ')'.

For example:

value = 'prefix(foo) bar(foo)';

return value.match( /prefix\(([^)]+)\)/g )  

result: 'prefix(foo)'


What I am trying to achieve is this:

value = 'prefix() bar(foo)';

return value.match( correctRegularExpression ) 

result: 'prefix()'


I am searching for correctRegularExpression and I am really stuck here since I am new to regular Expressions.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
Marten Zander
  • 2,105
  • 3
  • 15
  • 31

1 Answers1

0

Use capture groups in your regex like so:

var value = 'prefix(hello) foo(goodbye)';

var matches = value.match(/prefix\((\w*)\)/);

console.log(matches[1]);

Read more about it here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match

Jack
  • 784
  • 6
  • 18