Consider the following code:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var str = '<12> rnbqkb-r Rnbq-b-r ';
var pat1 = new RegExp('^\\<12\\> ([rnbqkpRNBQKP-]{8}) ([rnbqkpRNBQKP-]{8})');
var pat2 = new RegExp('^\\<12\\> ([rnbqkp RNBQKP-]{8}){2}');
var pat3 = new RegExp('^\\<12\\> ([rnbqkp RNBQKP-]{8}){2}?');
document.write(str.match(pat1));
document.write('<br />');
document.write(str.match(pat2));
document.write('<br />');
document.write(str.match(pat3));
</script>
</body>
</html>
which produces
<12> rnbqkb-r Rnbq-b-r,rnbqkb-r,Rnbq-b-r
<12> rnbqkb-r Rnbq-b-, Rnbq-b-
<12> rnbqkb-r Rnbq-b-, Rnbq-b-
as output.
Why does neither pattern pat2
nor pat3
capture the first group rnbqkb-r
? I would like to capture all groups without having to repeat them explicitly as in pattern pat1
.