为什么js正则表达式会在换行符上冻结浏览器?

时间:2017-04-20 14:00:31

标签: javascript regex

当我选择一行时,它工作正常,但如果我选择几行,它会开始处理数据并永远加载处理器。我认为正则表达式并没有看到新线的符号。那我该如何解决呢?



 function Selection () {
               
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    
     html = html.replace(/(\r\n|\n|\r)/gm,"");
                       
    var patt1 = /id=\"load+(.*)+<\/a>/g;
    var result = html.match(patt1);
    alert(result);     

        }     
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>

<div class="text" onmouseup="Selection()">
You &nbsp;
<br><a class="good" data-id="1114" href="#load" id="load1114">are very </a> important for us.
<br>So we're offering you 
<br><a class="good" data-id="1114" href="#load" id="load1114"> win-win</a> option.
<br>You will get 2 refrigerators if you buy one

</div>

</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案