To extract html part from string :
With escape in regular expression:
RegExp('<script type="text\/javascript">[^]+<\/script>');
var content = '<p>test</p><script type="text/javascript">somany lines and \n\
so many lines</scr' + 'ipt>';
var reg_escape = new RegExp('<script type="text\/javascript">[^]+<\/scr' + 'ipt>');
var onlyHtml = content.replace(reg_escape,"");
alert(onlyHtml);
Without escape in regular expression:
RegExp('<script type="text/javascript">[^]+</script>');
var content = '<p>test</p><script type="text/javascript">somany lines and \n\
so many lines</scr' + 'ipt>';
var reg_escape = new RegExp('<script type="text/javascript">[^]+</scr' + 'ipt>');
var onlyHtml = content.replace(reg_escape,"");
alert(onlyHtml);
Both of them get the same result--extracting html part only. Now there is a whole html file with escape in regular expression:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type='text/css'>
div#html{
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='content'>
</div>
<script type='text/javascript'>
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,"");
alert(onlyHtml);
</script>
</body>
</html>
Save as with_escape.html
and open it with a browser,you extract html part from with_escape.html
.
There is a whole html file without escape in regular expression:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type='text/css'>
div#html{
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='content'>
</div>
<script type='text/javascript'>
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,"");
alert(onlyHtml);
</script>
</body>
</html>
Save as without_escape.html
and open it with a browser,you get can't extract html part from without_escape.html
.An error ocurrs:
Why in previous code snippet ,it's no matter whether to escape \
as /\
or not?