나는 내가 작성한 스크립트 #에서이문자열을 이스케이프 처리하면 정규식이 엉망이되는 이유는 무엇입니까?
function ParseJsonDate(input) {
var tmp = +input.replace(/\/Date\((-?\d+)\)\//, '$1');
jsonDate = new Date(tmp);
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;
}
이 모양이로 컴파일
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;
}
그래도 디버거를 사용하여 단계를 밟으면 이스케이프 처리되지 않은 문자열에서 생성 된 RegExp 객체는 완전히 다릅니다.
아이디어가 있으십니까?
그 트릭을했습니다. 마지막으로 "패턴"은 다음과 같습니다 :'@ "\/Date \ ((-? \ d +) \) \ /"' –