in reply to Re: Can qr be un-compiled?
in thread Can qr be un-compiled?

my $cregexp = qr/[a-z].*?[0-9] $/is; print $cregexp; prints: (?si-xm:[a-z].*?[0-9] $)
JavaScript doesn't understand regexps that look like that:-
http://docs.sun.com/source/816-6408-10/regexp.htm


Lyle
-Update: ikegami's original response didn't have an example, hence mine.
ikegami - It would be nice if you listed updates you make to your replies

Replies are listed 'Best First'.
Re^3: Can qr be un-compiled?
by ikegami (Patriarch) on Feb 03, 2009 at 14:30 UTC

    I don't immediately have the means of converting a Perl regexp pattern into a JavaScript regexp pattern, but I got you half way there by answering your question. "[a-z].*?[0-9] $" would not be equivalent to the compiled regexp.

    If you're dealing with the subset that's compatible to both, why don't you just keep the pattern and the compiled regexp separately?

    my %regexps = map { $_ => qr/$_/ } @regexps;
      The problem is I'm dealing with someone else's compiles regexps that are passed to me, so I don't have the original string :(

        If it's just the flags, you can remove them yourself.

        my %on_flags; my %off_flags = map { $_ => 1 } split //, 'xism'; my $pattern = "$compiled"; if ($pattern =~ /^\(\?([a-z]+)(?:-([a-z])+)?:(.*)\)\z/s) { (my $on_flags, my $off_flags, $pattern) = ($1, $2||''); %on_flags = map { $_ => 1 } split //, $on_flagfs; delete @off_flags{keys %on_flags}; %off_flags = map { $_ => 1 } split //, $off_flags; delete @on_flags{keys %off_flags}; } ...generate JavaScript code here...

        Note: Doesn't handle other features of Perl regexps not present in JavaScript. There's a regexp parser module on CPAN (whose name escapes me) that would be more useful to you if you want to handle more.

        Note: Since it expects a compiled pattern, it doesn't handle $compiled = '(?i:foo)bar\)';