-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
  • 607,720
  • 39
  • 448
  • 563
Marten Zander
  • 2,385
  • 3
  • 17
  • 32

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
  • 804
  • 7
  • 18