-1

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:

enter image description here

Why in previous code snippet ,it's no matter whether to escape \ as /\ or not?

showkey
  • 482
  • 42
  • 140
  • 295

1 Answers1

0

In a JS string literal / and \/ mean exactly the same thing.

In an HTML document </script> is an end tag for a script element, but <\/script> isn't.

The version without the \ fails when you embed it in an HTML document because you are terminating the script element in the middle of an expression.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335