[英]Regex - match string with special characters between delimiter (html <style> tag)
我正在尋找一種解決方案,以在包含適當HTML模板的API響應中將html樣式標簽與任意文本(也是特殊字符)進行匹配:
字符串的一部分:
...
Aenean <style>asdfasdf</style>lacinia bibendum <a href="/life">life</a>
sed <style>asdfasdf</style>consectetur.
<a href="/work">Work</a> quis risus eget urna mollis ornare
<a href="/about">about</a> <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->leo.
...
我的正則表達式(到目前為止): <style[^>]*>(.*?)<\\/style>
僅匹配沒有特殊字符<style>asdfasdf</style>
的樣式標簽,但仍然缺少
<style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
要測試: https ://regexr.com/-如上所述的regex表達式和文本。
完整的JavaScript序列可直接將粘貼復制到您的代碼中:
console.warn(value);
console.warn(/<style[^>]*>(.*?)<\/style>/gs.test(value));
value = value.replace(/<style[^>]*>(.*?)<\/style>/gs, '');
console.warn(value);
console.warn(/<style[^>]*>(.*?)<\/style>/gs.test(value));
返回:字符串true字符串false
我認為您正在尋找s
regex標志,該標志允許匹配跨線邊界:
類似於(?s)<style[^>]*>(.*?)<\\/style>
,請在此處嘗試。
( /<style[^>]*>(.*?)<\\/style>/gs
在JS語法中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.