Javascript Regexp loop all matches

前端 未结 6 1930
名媛妹妹
名媛妹妹 2021-02-06 22:16

I\'m trying to do something similar with stack overflow\'s rich text editor. Given this text:

[Text Example][1]

[1][http://www.example.com]

I

相关标签:
6条回答
  • 2021-02-06 22:29

    Another way to iterate over all matches without relying on exec and match subtleties, is using the string replace function using the regex as the first parameter and a function as the second one. When used like this, the function argument receives the whole match as the first parameter, the grouped matches as next parameters and the index as the last one:

    var text = "[Text Example][1]\n[1][http: //www.example.com]";
    // Find resource links
    var arrMatch = null;
    var rePattern = new RegExp("\\[(.+?)\\]\\[([0-9]+)\\]", "gi");
    text.replace(rePattern, function(match, g1, g2, index){
        // Do whatever
    })
    

    You can even iterate over all groups of each match using the global JS variable arguments, excluding the first and last ones.

    0 讨论(0)
  • 2021-02-06 22:32

    I managed to do it in the end with this:

    var Text = "[Text Example][1]\n[1][http: //www.example.com]";
    // Find resource links
    reg = new RegExp(
      "\\[(.+?)\\]\\[([0-9]+)\\]",
      "gi");
    var result;
    while (result = reg.exec(Text)) {
      var LinkText = result[1];
      var Match = result[0];
      var LinkID = result[2];
      var FoundURL = new RegExp("\\[" + LinkID + "\\]\\[(.+?)\\]", "g").exec(Text);
      Text = Text.replace(Match, '<a href="' + FoundURL[1] + '" rel="nofollow">' + LinkText + '</a>');
    }
    console.log(Text);

    0 讨论(0)
  • 2021-02-06 22:34

    Here we're using exec method, it helps to get all matches (with help while loop) and get position of matched string.

        var input = "A 3 numbers in 333";
        var regExp = /\b(\d+)\b/g, match;
        while (match = regExp.exec(input))
          console.log("Found", match[1], "at", match.index);
        // → Found 3 at 2 //   Found 333 at 15 
    
    0 讨论(0)
  • 2021-02-06 22:37

    This format is based on Markdown. There are several JavaScript ports available. If you don't want the whole syntax, then I recommend stealing the portions related to links.

    0 讨论(0)
  • 2021-02-06 22:46

    Here's somw small example I hope you can find useful. \number is used in regex to refer a group match number and $number is used in the replace function to refer group results so you can enforce that numbers will be the same with something like that if your text is

    [Text Example][1]\n[1][http://www.example.com]
    

    it will match and if it is

    [Text Example][1]\n[2][http://www.example.com]
    

    it won't

    var re = /\[(.+?)\]\[([0-9]+)\s*.*\s*\[(\2)\]\[(.+?)\]/gi;
    var str = '[Text Example][1]\n[1][http://www.example.com]';
    var subst = '<a href="$4">$1</a>';
    
    var result = str.replace(re, subst);
    console.log(result);

    0 讨论(0)
  • 2021-02-06 22:47

    I agree with Jason that it’d be faster/safer to use an existing Markdown library, but you’re looking for String.prototype.replace (also, use RegExp literals!):

    var Text = "[Text Example][1]\n[1][http: //www.example.com]";
    var rePattern = /\[(.+?)\]\[([0-9]+)\]/gi;
    
    console.log(Text.replace(rePattern, function(match, text, urlId) {
      // return an appropriately-formatted link
      return `<a href="${urlId}">${text}</a>`;
    }));

    0 讨论(0)
提交回复
热议问题