2

How to catch alternative into the nested group?

I want to return first capturing group of: (.*)((core\/)?\/misc\/drupal\.js) (i.e. anything before the string core/misc/drupal.js which may or might not contain core/).

However, the core/ part always gets included to the first capturing group and not the second as supposed.

See also this for an example.

smihael
  • 863
  • 13
  • 29

1 Answers1

1

There is a problem in your regex : you have one slash character at the end of the (core\/)? group, followed by another slash character in \/misc\/drupal\.js. This causes the core/ part to be parsed in the first dot rule no matter what.

Removing the additional slash is not enough though. To avoid the dot eating up as many characters as possible, you have to disable the default greedy behaviour by adding a ? to the * quantifier, like so :

(.*?)((core\/)?misc\/drupal\.js)

See it in action here.

ttzn
  • 2,543
  • 22
  • 26