Incognito has asked for the wisdom of the Perl Monks concerning the following question:

The following regular expression matches MOST object initializer regular expressions written in JavaScript.

qr{/(?:[^\\/]|\\.)*/[gi]*};

This basically matches the following JavaScript code:

var objRegex = /"/; var objRegex = /'/; var objRegex = /\\/; var objRegex = /123[abc]456/; var objRegex = /\\\\/; var objRegex = /te'st\/as"df/; var objRegex = /123[\/]456/;

BUT fails to match on a valid Regex like this:

var objRegex = /123[/]456/;
where there is a single "/" character embedded in [] (this is apparently valid because most special characters do not have to be escaped inside [])... If the regex had the "/" character escaped, then this wouldn't be an issue.

Does anyone know how to improve the regex to match any valid JavaScript object intializer regular expression? (which seem pretty much identical to Perl regular expressions)?

Fixed disappearing square brackets 2002-02-14 dvergin

Replies are listed 'Best First'.
Re: Matching an JavaScript object initalizer regex
by japhy (Canon) on Feb 14, 2002 at 04:13 UTC
    I have a full-fledged regex parser, and a newer, better interface for it coming out "soon". In the meantime, you might want something like:
    qr{ / (?: # char class \[ \^? ]? (?: [^]\\]+ | \\. )* ] | # other schtuff (?: [^[\\\/]+ | \\. )* )* / }x;
    It looks right to me. I didn't know a delimiter didn't need quoting in a javascript character class...

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who could use a job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Thanks for the response. The regex looked promising, but unfortunately, when run on my sample test cases, the regex matches less that the original regex did...

      It doesn't seem to work on the simple ones like:

      var objRegex = /"/; var objRegex = /123[abc]456/; var objRegex = /te'st\/as"df/;
      Of course, this is almost out of my league now (it took me quite a while just to grasp the original one)...
        Sorry! I left out the ^ in my character class. I've updated it.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Matching an JavaScript object initalizer regex
by little (Curate) on Feb 14, 2002 at 03:50 UTC
    They do not only seem pretty much alike. JavaScript uses the same RE as Perl or grep does.
    Which lightens the burden of your problem as you now only need a Perl RE parser. :-)
    Search for that in the search box and you'll get more then a few nodes worth reading :-)

    Have a nice day
    All decision is left to your taste
      > They do not only seem pretty much alike. JavaScript uses the same RE as Perl or grep does.

      Well technically, they don't. Perl's regexp engine is *far* advanced from that in Javascript (up to version 1.3 at least, 1.5 is a bit better). They all have the same basic features (and in grep's case they're *very* basic, egrep is a little more capable), and sometimes even they aren't standard. But Perl is in a league of it's own when it comes to complex regular expressions and speed. Just check out the Extended Features section under man perlre for many examples of features not available both Javascript and grep.
      HTH

      broquaint

        I do fully agree.
        But the fact that counts here is simly that Perl will "get" any RE from grep or JavaScript as long as the RE itself was written according to what is commonly referred to as "proper notation", even if a simple one.
        All animals capable of flying have wings, but not all animals which have wings can fly.

        Have a nice day
        All decision is left to your taste