Page 1 of 1

deal with quoted str

Posted: Tue Jan 21, 2020 1:13 am
by coater
if I replace ' with \' in a delphi str and then execute in JS, it will run wrong because delphi will replace ' with ''.

see:
delphi str:
R de cop a k'a d complex

GetTxtJavaStr := 'TitleFRef = ' + DealJsSpeChar(TitleFRef) +';'
Chromium1.Browser.MainFrame.ExecuteJavaScript(GetTxtJavaStr , 'about:blank', 0 );

function DealJsSpeChar(Astrforjs: string): string;
var JsSpeCharList: TstringList;
i:integer;
begin
result := '';
JsSpeCharList:= TStringList.Create;
//['\', CHAR(39), '"', '&', '\\'] ; +','+ [ '\' +','+ CHAR(39) +','+ '"' +','+ '&' +','+ '\\' ]
//JsSpeCharList.Delimiter:=','; // CHAR(39):'; CHAR(34) "; CHAR(38) &; CHAR(92) \;
//JsSpeCharList.DelimitedText := CHAR(34) +','+ CHAR(38) +','+ CHAR(39) +','+ CHAR(92) ;
for i := 0 to 100 do
begin
if i in [34, 38, 39, 92] then JsSpeCharList.Add(CHAR(i));
end;
for i := 0 to JsSpeCharList.Count-1 do
begin
if AnsiPos(JsSpeCharList.Strings,Astrforjs )>0 then
result := StringReplace(Astrforjs, JsSpeCharList.Strings, '\'+ JsSpeCharList.Strings,[rfReplaceAll])
end;
result := QuotedStr(result);
FreeAndNil(JsSpeCharList);
end;

Re: deal with quoted str

Posted: Tue Jan 21, 2020 1:38 am
by coater
result := char(39) + result + char(39); should be ok!