正規表現の否定先読みを使って特定の文字列を含まない場合にのみ一致 - JavaScript
published:
2018.03.12 /
modified:
match は、正規表現オブジェクトを使って文字列の一致・不一致のチェックを行えるメソッド。
以下は、正規表現の否定先読みを使って、特定の文字列を含まない場合にマッチし、結果を返している。
console.log("big apple and small remon".match(/^(?!.*apple).*$/));
// null
console.log("big remon and small remon".match(/^(?!.*apple).*$/));
// ["big remon and small remon"]