直接上代码:
//需要引用单元文件 System.RegularExpressions
function TForm1.MyMatchEvaluator(const Match: TMatch): string;
var
escape: string;
begin
escape := Match.Value;
if escape.StartsWith('\u') then
begin
var by: TBytes := TEncoding.UTF8.GetBytes(WideChar(StrToInt('$' + Copy(escape, 3, 4))));
if IsDBCSLeadByte(by[0]) then
Result := TEncoding.Unicode.GetString(by)
else
Result := StringOf(by);
end
else if escape = '\r' then
Result := #13
else if escape = '\n' then
Result := #10
else if escape = '\t' then
Result := #9
else if escape = '\\' then
Result := '\'
else if escape = '\"' then
Result := '"'
else
Result := escape;
end;
var
regex: TRegEx;
begin
regex := TRegEx.Create('(\\[rnt\''\"]|\\u[0-9a-fA-F]{4})');
//aResultObjectAsJson是web中时行转义后的字符串
mmo1.Lines.Text := regex.Replace(aResultObjectAsJson, MyMatchEvaluator);
end;