简体   繁体   English

JavaScript:正则表达式将字符串从一个标记删除到另一个标记

[英]JavaScript: Regex to remove string from one tag to another

I have a string 我有一个字符串

 <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> 

in this I want to remove everything inside the style tags. 在这里我想删除样式标签内的所有内容。 Can anyone please tell me the optimal solution.The expected output is: 谁能告诉我最佳解决方案。预期输出是:

 <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> 

There is no need for regex since there are DOM methods available to deal with it. 不需要正则表达式,因为有可用的DOM方法来处理它。

Create a dummy div element and use querySelector to remove the child style element. 创建一个虚拟div元素并使用querySelector删除子style元素。

var div = document.createElement( "div" );
div.innerHTML = str; //your input string
div.removeChild( div.querySelector( "style" ) );
console.log( div.innerHTML );

Demo 演示

 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 ); 

If you can use jquery then you don't need to use regex. 如果你可以使用jquery,那么你不需要使用正则表达式。

$(document).ready(function(){
 $('style').remove();
});

Here is the regex you are looking for. 这是你正在寻找的正则表达式。 Short and simple. 简短而简单。

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM