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?
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:Ironically, this regex matches a string that starts and ends with
/. That was your intention, wasn't it?