Javascript RegEx inside Perl. Escape Character Madness!

So, I have a perl script that's outputting a page (js functions + html).

I'm running into a problem, though.

What's in my code isn't what comes out in the actual code of the page.

I realized it's probably because of escape characters.

Here's the javascript file:

[html]
function xtractFile(data)
{
var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
return {path: m[1], file: m[2]}
}
[/html]


and here's the html source of the page after being generated:

[html]
var m = data.match(/(.*)[/\]([^/\]+.w+));
[/html]


the regex is stripping out the filename from a full path.

i.e. C:\Folder1\Folder2\Folder3\filename.pdf becomes filename.pdf


I've been doing OK with Perl, but regex is really throwing me.

I'm sure I'm going to come across this again in the future, so I was hoping I might get some enlightenment on how to handle this kind of situation.

Comments

  • Perl evaluates your output and "Escape-Symbol" combination becomes - well, whatever it should be, in most cases just
    "Symbol". So, if you need it verbatim Escape an Escape! Like that:

    var m = data.match(/(.*)[\\/\\\\]([^\\/\\\\]+\\.\\w+)$/);

    Every \\ will be evaluated to a single \
  • Figured this out via another forum, but I wanted to post what I found.


    I needed to keep my javascript regex out of the perl interpolation.

    [html]

    $javascript = <<'JAVASCRIPT';

    function xtractFile(data)
    {
    alert ("Data&colon; " + data);
    var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
    alert (m[1]);
    alert (m[2]);
    // return {path: m[1], file: m[2]};
    }

    JAVASCRIPT
    [/html]

    Then right under it I start the regular HTML where I actually need some perl variables.



    [html]
    print <<"END";


    File Upload Utility

    ...

    <SCRIPT LANGUAGE="JavaScript">
    function example(){
    alert ($PerlVariable);
    }

    END
    [/html]


    I didn't know you could split it up into sections like that.

    Hopefully this will help someone else.
  • You can even mix'em up! Try the following sample, see what happens

    $not = '';
    print <<BOO, <<'FOO';
    Section BOO is $not evaluated
    BOO
    Section FOO is $not evaluated
    FOO


    Isn't Perl wonderful? Smiley Happy
  • You can even mix'em up! Try the following sample, see what happens

    $not = '';
    print <

    Isn't Perl wonderful? Smiley Happy
    you are evil. scaring people. Smiley Wink
  • you are evil. scaring people. Smiley Wink
    Thou Shalt Know and Love Thy Tools Smiley Happy
  • You can even mix'em up! Try the following sample, see what happens. Isn't Perl wonderful? Smiley Happy
    I've got a feeling that one isn't recommended by "Perl Best Practices" (the book) ;-)
  • I've got a feeling that one isn't recommended by "Perl Best Practices" (the book) ;-)
    Probably not Smiley Happy)
  • I've got a feeling that one isn't recommended by "Perl Best Practices" (the book) ;-)
    Practices-shmractices....who cares?! as long it's effective Smiley Happy)))
Sign In or Register to comment.