while( $code !~ m#\G$#gc ) { if( $code =~ m#\G//(.*)\n#gc ) { # $1 is end-of-line comment } elsif( $code =~ m#\G"((?:[^"\\]|\\.)*)"#gc ) { # $1 is the inside double quotes } elsif( $code =~ m#\G'((?:[^'\\]|\\.)*)'#gc ) { # $1 is the inside single quotes } elsif( $code =~ m#\G/*(.?)*/#gmc ) { # $1 is a comment } elsif( $code =~ m#\G/((?:[^/\\]|\\.)*)/#gc ) { # $1 is a regex } elsif( $code =~ m#\G([^/'"]+)#gc ) { # $1 is "other code" } elsif( $code =~ m#\G/#gc ) { # division, we hope. } else { # We have hit invalid code? } }