|
|
|
NOTE 1 NOTE 2 is repeated the number of times specified by the . A can be non-greedy, in which case the pattern is repeated as few times as possible while still matching the sequel, or it can be greedy, in which case the pattern is repeated as many times as possible while still matching the sequel. The pattern is repeated rather than the input character sequence that it matches, so different repetitions of the different input substrings. If the and the sequel of the regular expression all have choice points, the is first matched as many (or as few, if non-greedy) times as possible. All choices in the sequel are tried before moving on to the next choice in the last repetition of th ) repetition of are tried before moving on to the next choice in the next-to-last (n - 1) st repetition of ; at which point it may turn out that more or fewer repetitions of are now possible; these are exhausted (again, starting with either as few or as many as possible) before moving on to the next choice in the (n - 1) st and so on. Compare which returns "abcde" with which returns "abc" . Consider also which, by the choice point ordering above, returns the array and not any of: The above ordering of choice points can be used to write a regular expression that calculates the which returns the gcd in unary notation "aaaaa" . /a[a-z]{ 2 , 4 }/.exec( "abcdefghi" ) /a[a-z]{ 2 , 4 }?/.exec( "abcdefghi" ) /(aa|aabaac|ba|b|c)*/.exec( "aabaac" ) [ "aaba" , "ba" ] [ "aabaac" , "aabaac" ] [ "aabaac" , "c" ] "aaaaaaaaaa,aaaaaaaaaaaaaaa" .replace( /^(a+)\1*,\1+$/ , "$1" ) 589 |