Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Python versus Javascript Regex

Last post 04-16-2012, 11:31 AM by Stevezilla00. 2 replies.
Sort Posts: Previous Next
  •  05-13-2011, 4:46 AM 82041

    Python versus Javascript Regex

    Hello,

    The following code in Python returns what I expect:

    m = re.match(r'(?:(A+)|(B+))+','AB')

    print (m.group(),m.group(1),m.group(2)) -> ('AB', 'A', 'B')

    But in Javascript, I'v got a non expected 'undefined'   

    var m = /(?:(A+)|(B+))+/.exec('AB');

    alert (m[0] + ',' + m[1] + ',' + m[2]);   ->   AB,undefined,B

    Could you explain the js behavior and how to fix it to have Python like result.

    Thank you

     

    Filed under:
  •  05-14-2011, 3:05 PM 82098 in reply to 82041

    Re: Python versus Javascript Regex

    This is actually a browser issue in how they choose to implement their JavaScript regex engine.  For groups that don't participate in the match they either return null or undefined. For your pattern in the first pass the "A" match and the second group isn't used, the qualifier cause the entire pattern to be run again resetting the groups, second pass the first group doesn't participate so it return undefined. 

    This is just one of many differences across platforms.  For a much deeper explantion of the issue see http://blog.stevenlevithan.com/archives/npcg-javascript


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  04-16-2012, 11:31 AM 84968 in reply to 82098

    Re: Python versus Javascript Regex

    Your analysis of the first and second passes is correct. However, this isn't really a browser issue--it follows the ECMAScript specification. And although it's true that the result is undefined because at the end you're left with a nonparticipating capturing group, the reason things end up that way is more specific. I've previously discussed it under the heading "Change the behavior of backreference resetting during subpattern repetition" at  http://blog.stevenlevithan.com/archives/fixing-javascript-regexp
    My regex-centric blog :: JavaScript regex tester
View as RSS news feed in XML