cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
lala
Level VII

How do replace these with JSL with regex substitution?

This is copied in the browser. How to use JSL's regular substitution is processed into syntactic JSL with the following requirements:
1, contains POST......HTTP/1.1 replaced with: u="...";h=[=>];
2, containing:, write h
3. The last line..., replace with: jj="...";rs=New HTTP Request(URL(u),Method("POST"),JSON(jj),Headers(h));

Thanks!

 

POST https://app.com/w1/api HTTP/1.1
Host: apparticle.longhuvip.com
Connection: keep-alive
Content-Length: 87
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://app.com
User-Agent: Mozilla/5.0 (Linux); 5.11.0.6
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: https://appp.com/w33/community/refreshKey=1696089600
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: com.aiyu.kaipanla

c=ForumsMsgJX&a=GetInfo&MsgID=30927&Tag=1&DeviceID=ffffffff-e4d8-03bc-ffff-ffffb561792e
11 REPLIES 11
lala
Level VII

回复: How do replace these with JSL with regular substitution?

2023-10-10_9-08-04.png

u="https://app.com/w1/api";h=[=>];
h["Host"]="apparticle.longhuvip.com";
h["Connection"]="keep-alive";
h["Content-Length"]="87";
h["Accept"]="application/json, text/javascript, */*; q=0.01";
h["User-Agent"]="Mozilla/5.0 (Linux); 5.11.0.6";
h["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";
h["Accept-Encoding"]="gzip, deflate";
h["Accept-Language"]="zh-CN,en-US;q=0.8";
h["X-Requested-With"]="com.aiyu.kaipanla";

jj="c=ForumsMsgJX&a=GetInfo&MsgID=30927&Tag=1&DeviceID=ffffffff-e4d8-03bc-ffff-ffffb561792e";
rs=New HTTP Request(URL(u),Method("POST"),JSON(jj),Headers(h));
rr=rs<<Send;txt=blobtochar(Gzip Uncompress(rr));
lala
Level VII

回复: How do replace these with JSL with regular substitution?

text=Get Clipboard();
txt = Regex(text, "^POST (.+?) HTTP\/1.1\!n", "u=\!"\1\!";h=[=>];");
txt = Substitute(text, "^(.+?): (.+?)\!n", "h[\!"\1\!"]=\!"\2\!";");

??

Thanks Experts!

2023-10-10_11-42-00.png

jthi
Super User

Re: How do replace these with JSL with regex substitution?

I would use Words() and split it so I have each of the lines as its own item in a list. Then I would loop over those and build the associative array as I go. Split on ":", first part as key and second as value (format as needed). Last line I would ignore in the loop and build separately

-Jarmo
lala
Level VII

Re: How do replace these with JSL with regex substitution?

  • Thanks jthi! Can you provide JSL? I can only use EmEditor.

jthi
Super User

Re: How do replace these with JSL with regex substitution?

This might give some idea

Names Default To Here(1);

aa_headers = Associative Array();

str = "POST https://app.com/w1/api HTTP/1.1
Host: apparticle.longhuvip.com
Connection: keep-alive";



For Each({line}, Words(str, "\!N"),
	rgx_match = Regex(line, "^.*?: ");
	If(!IsMissing(rgx_match), // not the most robust
		aa_headers[Word(1, line, ":")] = Substitute(line, rgx_match, "");
	);
);

show(aa_headers);

creating a list of possible keys for associative array is most likely more robust if it stays the same

-Jarmo
lala
Level VII

Re: How do replace these with JSL with regex substitution?

  • Continue asking: How can I get the total number of str lines?

Thanks Experts!

str = "POST https://app.com/w1/api HTTP/1.1
Host: apparticle.longhuvip.com
Connection: keep-alive";
jthi
Super User

Re: How do replace these with JSL with regex substitution?

Scripting index 

jthi_0-1696919659774.png

and JMP documentation N Items() has the information for you 

-Jarmo
lala
Level VII

Re: How do replace these with JSL with regex substitution?

  • I still don't get it.

Thanks!

2023-10-10_14-42-44.png

lala
Level VII

Re: How do replace these with JSL with regex substitution?

EmEditor's jsees 2023-10-10_14-52-13.png

 

document.selection.Replace("POST (.[^ ]{0,}) HTTP\\/1\\.1","u=\"\\1\";h=[=>];",eeReplaceAll | eeFindReplaceRegExp,0);
document.selection.Replace("^(.{0,}): (.{0,})$","h[\"\\1\"]=\"\\2\";",eeReplaceAll | eeFindReplaceRegExp,0);