找出完整的標籤
撰寫一個正規表示法來找出標籤 <style...>
。它應該要能配對完整的標籤:它可能沒有屬性 <style>
或有幾個屬性 <style type="..." id="...">
。
…但是正規表示法不應該配對 <styler>
!
例如
let regexp = /your regexp/g;
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
模式開頭很明顯:<style
。
…但我們不能簡單地寫 <style.*?>
,因為 <styler>
會與之匹配。
我們需要在 <style
後面加上一個空格,然後可以選擇加上其他內容或結束 >
。
在正規表示法語言中:<style(>|\s.*?>)
。
在行動中
let regexp = /<style(>|\s.*?>)/g;
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">