У меня есть строка
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<style>body p{max-width: 100% !important;height: auto!important;} div{max-width: 100% !important;height: auto!important;}span{max-width: 100% !important;height: auto!important;} h1{max-width:100% !important; height: auto!important;}h2{max-width: 100% !important;height: auto!important;}h3{max-width: 100% !important;height: auto!important;}h4{max-width: 100% !important;height: auto!important;}h5{max-width: 100% !important;height: auto!important;} img{max-width: 100% !important;height: auto!important;}iframe{max-width:100% !important;height: auto!important;}
</style><span style="background-color: rgb(68, 68, 255);">#followforfollow #likeforlike yo!</span>
<h2></h2>
<h3></h3>
<h2></h2>
<h1></h1><u></u>
в этом я хочу удалить все внутри тегов стиля. Может ли кто-нибудь сказать мне оптимальное решение. Ожидаемый результат:
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<span style="background-color: rgb(68, 68, 255);">#followforfollow #likeforlike yo!</span>
<h2></h2>
<h3></h3>
<h2></h2>
<h1></h1><u></u>
Вот регулярное выражение, которое вы ищете. Короткие и простые.
var str = '<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<style>body p{max-width: 100% !important;height: auto!important;} div{max-width: 100% !important;height: auto!important;}span{max-width: 100% !important;height: auto!important;} h1{max-width:100% !important; height: auto!important;}h2{max-width: 100% !important;height: auto!important;}h3{max-width: 100% !important;height: auto!important;}h4{max-width: 100% !important;height: auto!important;}h5{max-width: 100% !important;height: auto!important;} img{max-width: 100% !important;height: auto!important;}iframe{max-width:100% !important;height: auto!important;}
</style><span style="background-color: rgb(68, 68, 255);">#followforfollow #likeforlike yo!</span>
<h2></h2>
<h3></h3>
<h2></h2>
<h1></h1><u></u>';
//replace everything between <span></span>
str = str.replace(/<style>(.|\r?\n)*<\/style\>/,'');
console.log(str);
Нет необходимости в регулярном выражении, так как существуют доступные методы DOM.
Создайте элемент dummy div
и используйте querySelector
для удаления элемента дочернего style
.
var div = document.createElement( "div" );
div.innerHTML = str; //your input string
div.removeChild( div.querySelector( "style" ) );
console.log( div.innerHTML );
демонстрация
var str = '<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<style>body p{max-width: 100% !important;height: auto!important;} div{max-width: 100% !important;height: auto!important;}span{max-width: 100% !important;height: auto!important;} h1{max-width:100% !important; height: auto!important;}h2{max-width: 100% !important;height: auto!important;}h3{max-width: 100% !important;height: auto!important;}h4{max-width: 100% !important;height: auto!important;}h5{max-width: 100% !important;height: auto!important;} img{max-width: 100% !important;height: auto!important;}iframe{max-width:100% !important;height: auto!important;}
</style><span style="background-color: rgb(68, 68, 255);">#followforfollow #likeforlike yo!</span>
<h2></h2>
<h3></h3>
<h2></h2>
<h1></h1><u></u>';
var div = document.createElement( "div" );
div.innerHTML = str;
div.removeChild( div.querySelector( "style" ) );
console.log( div.innerHTML );
regex
Если вы можете использовать jquery, вам не нужно использовать регулярное выражение.
$(document).ready(function(){
$('style').remove();
});