Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

为何无法用正则表达式提取html?

show.html 包含下面的内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type='text/css'>
    div#html,div#css,div#js,div#run{
        border:1px solid red;
        height:80px;
        width:80px;
        float:left;
    }
    div#content{
        clear:both;
        width:400px;
        height:400px;
        border:1px solid black;
    }
   </style>
</head>
<body>
    <div id='html'>html</div>
    <div id='css'>css</div>
    <div id='js'>js</div>
    <div id='run'>run</div>
    <div id='content'>
    </div>        
    <script type="text/javascript">
    var html_string = document.body.innerHTML;
    var content = document.getElementById('content');
    var ob_html = document.getElementById('html');
    ob_html.onmouseover = function(){
        content.innerText = html_string; 
    } 
    ob_html.onmouseout = function(){
        content.innerText = '';
    }
   </script>    
</body>
</html>

浏览器代开后,鼠标进入div#html,显示结果是:
image

我希望仅仅显示 html部分:

    <div id='html'>html</div>
    <div id='css'>css</div>
    <div id='js'>js</div>
    <div id='run'>run</div>
    <div id='content'>
    </div> 

于是修改js部分

    var html_string = document.body.innerHTML;
    var content = document.getElementById('content');
    var ob_html = document.getElementById('html');
    var reg = new RegExp('<script type="text/javascript">.+</script>');
    var onlyHtml = html_string.replace(reg,"");
    ob_html.onmouseover = function(){
        content.innerText = onlyHtml; 
    } 
    ob_html.onmouseout = function(){
        content.innerText = '';
    }

为何不能提取出html部分?

var content = "<p>test</p><script type='text/javascript'>somany lines and 
              so many lines</script>"
var reg = new RegExp("<script type='text/javascript'>.+</script>");
var onlyHtml = content.replace(reg,"");
alert(onlyHtml);

上面可是可以提供出正确的结果的哈?!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.不匹配换行符

html.replace(/<script type="text/javascript">[^]+</script>/, '')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...