Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のようなパターンに対して行を一致させています。
if (/.*someRegExp(.*)someOtherRegExp.*/) { $1 を処理する }
しかし、問題は、行に「someRegExp(.*)someOtherRegExp」が多数出現していることです。
最初のオカレンスを確実に選択する方法を教えてください。
ありがとう!
正規表現のその部分の前に量指定子を消極的にします:
if (/.*?someRegExp(.*)someOtherRegExp.*/) { process $1 }
これで、.*?後続のパターンに一致する最初の部分文字列の前の文字列のみに一致します。
.*?