【发布时间】:2012-02-14 14:01:39
【问题描述】:
我正在尝试生成与此类似的 javascript 代码
function ParseJsonDate(input) {
var tmp = +input.replace(/\/Date\((-?\d+)\)\//, '$1');
jsonDate = new Date(tmp);
return jsonDate;
}
在脚本#中我写了这个:
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;
}
编译成这样:
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;
}
这看起来不错,但是当我使用调试器单步执行时,从非转义字符串构造的 RegExp 对象完全不同。
有什么想法吗?
【问题讨论】:
标签: javascript regex escaping script#