Why does unescaping a string mess up my regex?

853 views Asked by At

I'm trying to generate javascript code similar to this

function ParseJsonDate(input) {
    var tmp = +input.replace(/\/Date\((-?\d+)\)\//, '$1');
    jsonDate = new Date(tmp);

    return jsonDate;
}

In Script# I've written this:

    public static Date ParseJsonDate(string input)
    {
        string pattern = @"/\/Date\((-?\d+)\)\//".Unescape();
        RegularExpression regex = new RegularExpression(pattern);
        string tmp = input.ReplaceRegex(regex, "$1");
        Number milliseconds = Number.Parse(tmp);
        Date jsonDate = new Date(milliseconds);
        return jsonDate;
    }

Which compiles to this:

StringFunctions.parseJsonDate = function StringFunctions$parseJsonDate(input) {
    /// <param name="input" type="String">
    /// </param>
    /// <returns type="Date"></returns>
    var pattern = unescape('/\\/Date\\((-?\\d+)\\)\\//');
    var regex = new RegExp(pattern);
    var tmp = input.replace(regex, '$1');
    var milliseconds = Number.parse(tmp);
    var jsonDate = new Date(milliseconds);
    return jsonDate;
}

This looks fine but when I step through it with a debugger, the RegExp object that gets constructed from the unescaped string is totally different.

Any ideas?

2

There are 2 answers

1
Alan Moore On BEST ANSWER

In /\/Date\((-?\d+)\)\//, the leading and trailing / are not part of the regex, they're the delimiters of the regex literal. They tell the JavaScript interpreter where the regex starts and ends, just like quotation marks delimit string literals.

In the Script# code, you're creating the regex in the form of a string literal, not a regex literal. The compiler thinks the leading and trailing / are meant to match literal slashes. With those gone, there shouldn't be any need to escape literal slashes in the regex. That leaves you with:

@"/Date\((-?\d+)\)/"

Ironically, this regex matches a string that starts and ends with /. That was your intention, wasn't it?

0
Pointy On

You don't need the leading and trailing / characters when you build a regex from a string. In fact, not only do you not need them, you don't want them because the regex parser assumes you mean that you want it to match a leading and trailing / around the pattern.