最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex multiple captures again - Stack Overflow

programmeradmin2浏览0评论

Ok, I think I need to repost my question that was originally:

Javascript Regex group multiple

with a full example. I have:

        var text = ""+ 
            "<html>                           " +
            "  <head>                         " +
            "  </head>                        " +
            "  <body>                         " +
            "    <g:alert content='alert'/>   " +
            "    <g:alert content='poop'/>    " +
            "  </body>                        " +
            "</html>";

        var regex = /<([a-zA-Z]*?):([a-zA-Z]*?)\s([\s\S]*?)>/m;
        var match = regex.exec( text );
        console.log(match)

Output from console.log is:

The problem is that I am only getting the result for the first ... not the other... what can I do to be able to capture and walk over all stuff that matched?

Ok, I think I need to repost my question that was originally:

Javascript Regex group multiple

with a full example. I have:

        var text = ""+ 
            "<html>                           " +
            "  <head>                         " +
            "  </head>                        " +
            "  <body>                         " +
            "    <g:alert content='alert'/>   " +
            "    <g:alert content='poop'/>    " +
            "  </body>                        " +
            "</html>";

        var regex = /<([a-zA-Z]*?):([a-zA-Z]*?)\s([\s\S]*?)>/m;
        var match = regex.exec( text );
        console.log(match)

Output from console.log is:

The problem is that I am only getting the result for the first ... not the other... what can I do to be able to capture and walk over all stuff that matched?

Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Feb 5, 2013 at 12:17 mjsmjs 22.4k32 gold badges133 silver badges216 bronze badges 9
  • 12 PS: do not use regex to parse HTML. – m0skit0 Commented Feb 5, 2013 at 12:18
  • Do you have a better idea of doing what I am trying to do? That is get the tags of <g:alert .../> which can really look as <r:method ... or whatever .. – mjs Commented Feb 5, 2013 at 12:19
  • What are you trying to do? what results do you need, exactly? – Cerbrus Commented Feb 5, 2013 at 12:21
  • 1 Instead of regex you should use DOM functions to achieve this. – leftclickben Commented Feb 5, 2013 at 12:22
  • 2 Basically, using Regex are wrong for your intentions because you are dealing with nested structures, i.e. recursion. And regular expression is unable to do this. To explain this, You should first understand that a finite automaton (which is the data structure underlying a regular expression) does not have memory apart from the state it's in, and if you have arbitrarily deep nesting, you need an arbitrarily large automaton, which collides with the notion of a finite automaton. – StarPinkER Commented Feb 5, 2013 at 12:25
 |  Show 4 more comments

2 Answers 2

Reset to default 18

exec returns only ONE result at a time and sets the pointer to the end of that match. Therefore, if you want to get ALL matches use a while loop:

while ((match = regex.exec( text )) != null)
{
    console.log(match);
}

To get all matches at one shot, use text.match(regex), in which the regex has g (global flag) specified. The g flag will make match find all matches to the regex in the string and return all the matches in an array.

[edit] and that's why my example HAD a g flag set! [/eoe]

var text = ""+ 
           "<html>                           " +
           "  <head>                         " +
           "  </head>                        " +
           "  <body>                         " +
           "    <g:alert content='alert'/>   " +
           "    <g:alert content='poop'/>    " +
           "  </body>                        " +
           "</html>";

// Note the g flag
var regex = /<([a-zA-Z]*?):([a-zA-Z]*?)\s([\s\S]*?)>/gm;

var match = text.match( regex );
console.log(match);

SIMPLE TEST:

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var text = ""+ 
           "<html>                           " +
           "  <head>                         " +
           "  </head>                        " +
           "  <body>                         " +
           "    <g:alert content='alert'/>   " +
           "    <g:alert content='poop'/>    " +
           "  </body>                        " +
           "</html>";

// Note the g flag
var regex = /<([a-zA-Z]*?):([a-zA-Z]*?)\s([\s\S]*?)>/gi;

var n = text.match( regex );
alert(n);
}
</script>

working perfectly...

This is what works:

           var text = ""+
            "<html>                           " +
            "  <head>                         " +
            "  </head>                        " +
            "  <body>                         " +
            "    <g:alert content='alert'/>   " +
            "    <g:alert content='poop'/>    " +
            "  </body>                        " +
            "</html>";

        var regex = /<([a-zA-Z]*?):([a-zA-Z]*?)\s([\s\S]*?)>/g;
        var match = null;
        while ( (match = regex.exec( text )) != null  )
            console.log(match)

Notice the /g which seems to be neccessary

发布评论

评论列表(0)

  1. 暂无评论