レッスンに戻る

完全なタグを見つける

タグ <style...> を検索する正規表現を作成します。それにはタグ全体が含まれていなければなりません。つまり、属性がない <style> か、属性が複数ある <style type="..." id="...">、です。

…ただし、正規表現は <styler> には一致しません。

例えば

let regexp = /your regexp/g;

alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">

パターンの開始は明らかです: <style

…ただし、<styler> が一致しないように、単に <style.*?> と記述することはできません。

<style の後に空白があり、その後にさらに何かがあるか、終了の > が必要です。

正規表現の言語では次のようになります: <style(>|\s.*?>)

動作中の様子

let regexp = /<style(>|\s.*?>)/g;

alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">