how to get literal value of string without escaping backslash ?

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
pksingh041
Posts: 4
Joined: Fri Mar 23, 2018 9:50 am

how to get literal value of string without escaping backslash ?

Post by pksingh041 » Mon Dec 17, 2018 7:21 am

Hello Everyone,

In my Imacro Script we are getting a variable which has \(Backslash) as part of string. we have to split string after the backslash and need to second part only.
but whenever we try to split it just removes the \ and gives the complete string by just removing backslash.
var = domain\username and we need just username. below is the code I have been trying but it gives the result like doaminusername

SET !VAR1 EVAL("var s='{{username}}'; var x,y; y=s.split('\\\\'); y[1];")

but it gives string wth \ removed.

{{username}} variable is being taken from outside script with value like ABC\xyz and we need only xyz as result which has to be sent as variable again to username field in the webpage.
Please help me. Thanks in Advance.

Regards,
Piyush
Last edited by pksingh041 on Thu Dec 20, 2018 5:47 am, edited 3 times in total.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Getting Porblem with \(Backslash) as part of string

Post by chivracq » Mon Dec 17, 2018 11:19 am

pksingh041 wrote:
Mon Dec 17, 2018 7:21 am
Hello Everyone,

In my Imacro Script we are getting a variable which has \(Backslash) as part of string. we have to split string after the backslash and need to second part only.
but whenever we try to split it just removes the \ and gives the complete string by just removing backslash.
var = domain\username and we need just username. below is the code I have been trying but it gives the result like doaminusername

Code: Select all

SET !VAR1 EVAL("var s='{{username}}'; var x,y; y=s.split('\\\\'); y[1];")
but it gives string wth \ removed.

{{username}} variable is being taken from outside script with value like ABC\xyz and we need only xyz as result which has to be sent as variable again to username field in the webpage.
Please help me. Thanks in Advance.

Regards,
Piyush
CIM...! :mrgreen: (Read my Sig, which you should already have read, as the 'EVAL()' Statement you are trying to use comes from me... :roll: )

But yep, I can confirm that Behaviour indeed, I had done some quick Testing a few months ago, or maybe one year already, for some other Thread, trying to (quickly) find some "universal" Solution (on FF, probably using v8.8.2 and/or v8.9.7) for all the iMacros Folder Paths without knowing the (Windows) Username in advance, and I had indeed encountered that "Problem" also... But I didn't really "insist" that much at that time..., I would need to dig into it again, which I might do once you'll have mentioned your FCI...

Hum, and if you could correct the Typo also in your Thread Title...? (even if nobody is going to use "problem"/"porblem" as a Search Keyword to search the Forum... :wink: )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
pksingh041
Posts: 4
Joined: Fri Mar 23, 2018 9:50 am

Re: Getting Porblem with \(Backslash) as part of string

Post by pksingh041 » Wed Dec 19, 2018 7:09 am

Hi Chivracq,

Thanks for the reply.

below is the information.

Imacro Version 10.4
browser version IE 11 but we are doing testing in Imacro directly. we are running it directly.
windows 2012 R2 but also tested in the Windows 10 and got the same result.

Regards.
Piyush
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Getting Porblem with \(Backslash) as part of string

Post by chivracq » Wed Dec 19, 2018 4:04 pm

pksingh041 wrote:
Wed Dec 19, 2018 7:09 am
Hi Chivracq,

Thanks for the reply.

below is the information.

Imacro Version 10.4
browser version IE 11 but we are doing testing in Imacro directly. we are running it directly.
windows 2012 R2 but also tested in the Windows 10 and got the same result.

Regards.
Piyush
Ah OK, FCI mentioned, good, even if 2 days later, I guess you are not really "in a hurry", ah-ah...!
But OK, nothing "wrong" with iMB v10.4, I had mentioned I had also seen the "Behaviour" you describe in FF as well...

And yeah, but hum, I had asked you to correct the ugly Typo in your Thread Title but you didn't "comply"... I won't go and do any Digging/Testing in your Case if this Thread won't be found by other Users because you can't type 3 Words without a Typo..., sorry...
And correct Spelling is "iMacros" btw, this time you manage to squeeze 3 Typos in just one Word... (I'm already a bit "suspicious" about the "Quality" of your Coding if you manage to squeeze up to 3 Typos in some (Key)Words/Commands, oops...! :shock: )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
pksingh041
Posts: 4
Joined: Fri Mar 23, 2018 9:50 am

Re: how to get literal value of string without escaping backslash

Post by pksingh041 » Wed Dec 19, 2018 4:52 pm

Hi Chivracq,

I have changed the title if that helps. I tried to keep it as simple as possible. And I am in hurry it just forum dint send any notification. Also I am not expert so not sure about quality. We just need to use it for automating login process for a website.

Below is the code I am using. Tried using different method for splitting the {{Username}} but got no success. username we are getting as dom\Username and we need to send only username in the login page.

URL GOTO={{URL}}
WAIT SECONDS=2.0
PROMPT {{username}}
WAIT SECONDS=2.0

SET !VAR3 EVAL("var s=\"{{!VAR2}}\"; s.split('\\\\')[1];")

'SET !VAR1 EVAL("var s="'{{username}}'"; var x,y; y=s.split('\\\\'); y[1];")
'SET !VAR2 EVAL("var s=\"{{username}}\"; s.split(\" (\\", 1);")

PROMPT {{!VAR2}}
WAIT SECONDS=2.0
PROMPT {{!VAR3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:/login.fcc ATTR=NAME:USER CONTENT={{!VAR2}}
WAIT SECONDS=2.0
TAG POS=1 TYPE=INPUT:PASSWORD FORM=ACTION:/login.fcc ATTR=NAME:PASSWORD CONTENT={{pmpass}}
WAIT SECONDS=2.0
TAG POS=1 TYPE=BUTTON:SUBMIT FORM=ACTION:/login.fcc ATTR=NAME:submit


Thanks.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: how to get literal value of string without escaping backslash

Post by chivracq » Wed Dec 19, 2018 9:58 pm

pksingh041 wrote:
Wed Dec 19, 2018 4:52 pm
Hi Chivracq,

I have changed the title if that helps. I tried to keep it as simple as possible. And I am in hurry it just forum dint send any notification. Also I am not expert so not sure about quality. We just need to use it for automating login process for a website.

Below is the code I am using. Tried using different method for splitting the {{Username}} but got no success. username we are getting as dom\Username and we need to send only username in the login page.

Code: Select all

URL GOTO={{URL}}
WAIT SECONDS=2.0
PROMPT {{username}}
WAIT SECONDS=2.0

SET !VAR3 EVAL("var s=\"{{!VAR2}}\"; s.split('\\\\')[1];")

'SET !VAR1 EVAL("var s="'{{username}}'"; var x,y; y=s.split('\\\\'); y[1];")
'SET !VAR2 EVAL("var s=\"{{username}}\"; s.split(\" (\\", 1);")

PROMPT {{!VAR2}}
WAIT SECONDS=2.0
PROMPT {{!VAR3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:/login.fcc ATTR=NAME:USER CONTENT={{!VAR2}}
WAIT SECONDS=2.0
TAG POS=1 TYPE=INPUT:PASSWORD FORM=ACTION:/login.fcc ATTR=NAME:PASSWORD CONTENT={{pmpass}}
WAIT SECONDS=2.0
TAG POS=1 TYPE=BUTTON:SUBMIT FORM=ACTION:/login.fcc ATTR=NAME:submit
Thanks.
Woaw...!, "speedy" Follow-up now, good... (And I had seen your Reply a few hours earlier, but got some Friends popping by...)

OK, for Thread Title, even if it could use a Question Mark as you are asking a Qt and not sharing a 'How-to', but OK, never mind...
(And you could "try" to discover the ]CODE[ Forum Tags to format your Posts, just an Idea... :idea: )

I had also done a mini-bit of Testing before my Friends arrived, and hum..., looks pretty "strange" and a bit Buggy to me, I would think...
Up to 4x Layers of Backlash are automatically removed/ignored by iMacros and/or the JS Engine of the Browser...
(I do my Testing on Pale Moon v26.3.3 (=FF47) + iMacros for FF v8.8.2 + Win10_x64.)

=> Any 'EVAL()' Manipulation on '\' (for 'split()' or 'indexOf()') only "works" if the Input String is in the "ABC\\\\\xyz" Format, (with the Double Quotes included, seems to remove one extra Layer with Double Quotes)... => With 5x '\'...! :shock: , and the 'split()' then indeed needs 4x '\' for it to finally "see" ONE '\', and no Difference in my Tests if using Single Quotes or Escaped Double Quotes in the 'EVAL()' for Vars or withing any JS Function...

So I don't know where your "ABC\xyz" or "Domain\Username" comes from, not visible from your Script, but it looks to me like you need to find a way to add an extra 4x '\' for iMacros and 'EVAL()' and 'split()' to be able to "recognize" the '\' as a "valid" Char in a String...

Could maybe use some mini-'.BAT' File to get that "Domain\Username" Data (with or without the Double Quotes), and replace the '\' with some other Char and save it to some mini-local-temp-File or onto the OS Clipboard, or if you get "it" already from iMacros and some 'EXTRACT', pfff..., a bit cumbersome, but could copy it to the Clipboard, and do some DOS 'rename' from some '.BAT' File to retrieve back the new String from iMacros using the 'EXTRACT' Mechanism on the containing Folder (or the File directly if you already know how it will be called) using the 'URL GOTO=file:///' Protocol... But sounds pretty cumbersome to me, to be honest...

Arrggghhh...!, wanted to post the Script I had used for my Debug/Testing, but hum..., I'm playing some funny AI/Web-Automation Xmas Game with constantly 6-8 Scripts running in parallel and I hit at some point by mistake the 'Record' Button instead of 'Play' in exactly by very bad luck the PM Profile where I had been doing my Testing and the Script I had been using got lost... Grrr...! :cry: [One of my EnhReq's btw tja...!, (Item_17) to prevent stg like that to happen...!)

I'm "softly" alerting TechSup to have a look at your Thread, maybe they'll come up with some better Idea(s)...
(Even if they will say iMB v10.4 is a very old Version, and "my" v8.8.2 Version also, but I'm pretty sure all "later/latest" v10.x for CR/FF or v12.5 for iMB/IE still behave exactly the same...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
pksingh041
Posts: 4
Joined: Fri Mar 23, 2018 9:50 am

Re: how to get literal value of string without escaping backslash

Post by pksingh041 » Thu Dec 20, 2018 5:42 am

Hi chivracq,

As the variable is coming from outside the script and we cant change anything there . We have to do it in script only. If the variable had been something like domain\\username then using \\\\ we can split the string easily but with single backslash we are not able to perform any function for that string. As soon as it sees single \ it gets escaped and we get domainusername without any special character :(

Thanks.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: how to get literal value of string without escaping backslash ?

Post by chivracq » Thu Dec 20, 2018 1:11 pm

pksingh041 wrote:
Thu Dec 20, 2018 5:42 am
Hi chivracq,

As the variable is coming from outside the script and we cant change anything there . We have to do it in script only. If the variable had been something like domain\\username then using \\\\ we can split the string easily but with single backslash we are not able to perform any function for that string. As soon as it sees single \ it gets escaped and we get domainusername without any special character :(

Thanks.
Qt Mark in Thread Title: Check...! :D

Yeah, very strange that 5x '\' are needed for iMacros\'EVAL()' to be able to see one, that's why I suggested to replace that '\' Char outside iMacros using a '.bat'/'.cmd'/'.ps'/'DOS' File or Command... I've sent a "soft" Alert to TechSup to have a look anyway...

The "strange" thing is that iMacros is somewhat able to handle Paths with only 1x '\' like in '!FOLDER_DATASOURCE' for example (that I had used for my Tests)... (But as soon as 'EVAL()' is put into the game, then the Backslashes get stripped...)

But hum, stg else you could do, I guess the "Domain" part is always constant, or maybe you only have a short List of 3 possible Domains for example, then as a Workaround you could use 'replace()' in the 'EVAL()', x3 for all 3 Domains, and in the "correct" Order...! (If you have "Dom1" + "Dom11" + "Dom99", you'll need to handle "Dom11" before "Dom1" or you might end up with some "1Username" instead of the expected "Username".)
Or if all Domains all have the same Length, 6 or 8 Chars for example, then you could use 'substr()' or 'substring()' also, or in combination with 'replace()'.
And you could also use 'REGEXP' depending on what "Format" exactly is used for your Domains (Capitals/Numbers)...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: how to get literal value of string without escaping backslash ?

Post by chivracq » Thu Dec 20, 2018 3:36 pm

And some other Workaround would be to use some Online "Text Find & Replace" Service like this Site for example...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
SET !EXTRACT_TEST_POPUP NO
TAB T=1
URL GOTO=http://www.unit-conversion.info/texttools/replace-text/

SET Input "ABC\xyz"

TAG POS=1 TYPE=H1 ATTR=TXT:Replace<SP>text<SP>online

TAG POS=1 TYPE=LABEL FORM=ID:form ATTR=TXT:Input<SP>data
'TAG POS=1 TYPE=TEXTAREA FORM=ID:form ATTR=ID:field_text CONTENT=ABC\xyz
TAG POS=1 TYPE=TEXTAREA FORM=ID:form ATTR=ID:field_text CONTENT={{Input}}

TAG POS=1 TYPE=LABEL FORM=ID:form ATTR=TXT:Find<SP>text
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:form ATTR=ID:field_find CONTENT=\

TAG POS=1 TYPE=LABEL FORM=ID:form ATTR=TXT:Replace<SP>with
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:form ATTR=ID:field_replace CONTENT=#

TAG POS=1 TYPE=LABEL FORM=ID:form ATTR=TXT:Output:
WAIT SECONDS=1
'TAG POS=1 TYPE=TEXTAREA FORM=ID:form ATTR=ID:output CONTENT=ABC#xyz
TAG POS=1 TYPE=TEXTAREA FORM=ID:form ATTR=ID:output EXTRACT=TXT

SET Username EVAL("var s='{{!EXTRACT}}'; var z=s.split('#'); z[1];")

PROMPT Input:<SP>_{{Input}}_<BR>Output:<SP>_{{!EXTRACT}}_<BR>Username:<SP>_{{Username}}_
(Tested on iMacros for FF v8.8.2, PM v26.3.3 (=FF47), Win10_x64.)

And if you have a look at the Source of that Page/Site to find out how "they" do the 'replace()', that might give you some "Ideas" and you might be able to apply it yourself directly either in 'EVAL()' (though I guess you will then still encounter the same original Pb), or in 'URL GOTO=javascript:', but "communicating" between the 'URL GOTO=javascript:' and the "main" '.iim' to pass a Var is always a bit cumbersome... (You need to create or modify or add some Attribute to some HTML Element from the HTML Page to then using the iMacros Extract Mechanism re-extract that Field/Text/Attribute, but it will be a bit quicker than using some external Site though...)
Last edited by chivracq on Tue Jan 08, 2019 12:33 pm, edited 1 time in total.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Tom, Tech Support
Posts: 3834
Joined: Mon May 31, 2010 4:59 pm

Re: how to get literal value of string without escaping backslash ?

Post by Tom, Tech Support » Tue Jan 08, 2019 11:45 am

The problem with using a macro variable whose value contains a backslash inside of an EVAL command is that the backslash will always be interpreted as the escape character. So, rather than "seeing" for example "ipswitch\tom", this gets interpreted as "ipswitch om", where the empty space is actually a tab character as specified by \t. There is no inherent way around this.

You should also bear in mind that there is an extra level of indirection when using the EVAL command. Because the EVAL command accepts a string as its only parameter, iMacros interprets any backslash characters within this string as an escape character. So if you need to use a backslash in your Javascript code within the EVAL command, for example in a regular expression statement, you need to escape the backslash character itself as mentioned in Note #4 in the command reference. Going further, if you need a literal backslash inside of a Javascript string variable inside of your EVAL command, you'll end up with something like this:

Code: Select all

SET myDomainAndUser EVAL("var user = 'ipswitch\\\\tom'; user;")
PROMPT {{myDomainAndUser}}
Your best bet when your source data contains a backslash is to either a) get rid of that backslash before it even gets to your macro code, and if that is not possible then b) try to use a workaround like suggested by chivracq in his last two posts.
Regards,

Tom, iMacros Support
Post Reply