Zend certified PHP/Magento developer

ECMA Regex match everything till first occurrence of exact string

I need a regex to to match the first occurrence of the @endforeach and not let last.

This is the Regex that I’m currently trying: /(@(foreach|if)).*(@end2)/ The problem is it dose not stop matching till it gets to the last @endforeach

Model: ${Model.name}

Fields: [
@foreach (Model.fields as field)
@if ('${field.relationName}' === 'undefined')
    ${Model.name}.${field.name}
@endif
@endforeach
];

Relationships: [
@foreach (Model.fields as field)
@if ('${field.relationName}' !== 'undefined')
    ${Model.name}.${field.name}
@endif
@endforeach
];