繁体   English   中英

正则表达式从文本中捕获ID

[英]Regex to capture Ids from text

我在以下正则表达式中尝试捕获每个开始注释的ID。 但是由于某种原因,我只能捕获第一个。 它不会获取嵌套注释的ID。 它只会在控制台上打印1000。 我正在尝试使其同时捕获1000和2000。有人可以在我的正则表达式中发现错误吗?

<script type="text/javascript">

    function ExtractText() {
        var regex = /\<!--Start([0-9]{4})-->([\s\S]*?)<!--End[0-9]{4}-->/gm;
       var match;
        while (match = regex.exec($("#myHtml").html())) {
            console.log(match[1]);
        }
    }

</script>

<div id="myHtml">
   <!--Start1000-->Text on<!--Start2000-->the left<!--End1000-->Text on the right<!--End2000-->
</div> 

根据Mike Samuel的回答,我将JS更新为以下内容:

function GetAllIds() {

        var regex = /<!--Start([0-9]{4})-->([\s\S]*?)<!--End\1-->/g;
        var text = $("#myHtml").html();
        var match;
        while (regex.test(text)) {
            text = text.replace(
               regex,
               function (_, id, content) {
                   console.log(id);
                   return content;
               });
        }
    }

 <!--Start1000-->Text on<!--Start2000-->the left<!--End1000-->Text on the right<!--End2000--> 

“ 1000”区域与“ 2000”区域重叠,但是exec循环仅查找不重叠的匹配项,因为每次对具有相同正则表达式和字符串的exec调用均始于最后一个匹配项的末尾。 要解决此问题,请尝试

var regex = /<!--Start([0-9]{4})-->([\s\S]*?)<!--End\1-->/g;
for (var s = $("#myHtml").html(), sWithoutComment;
     // Keep going until we fail to replace a comment bracketed chunk
     // with the chunk minus comments.
     true;
     s = sWithoutComment) {
  // Replace one group of non-overlapping comment pairs.
  sWithoutComment = s.replace(
     regex,
     function (_, id, content) {
       console.log(id);
       // Replace the whole thing with the body.
       return content;
     });
  if (s === sWithoutComment) { break; }
}

您可以使用分组,然后使用另一个正则表达式:

var regex =  /(<!--Start)([0-9]{4})/ig;
var str = document.getElementById('myHtml').innerHTML;
var matches = str.match(regex);
for(var i=0;i<matches.length;i++){
    var m = matches[i];
    var num = m.match(/(\d+)/)[1];
    console.log(num);
}

暂无
暂无

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

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