How to check if a string contains backslash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2024 03:00 AM
Hi All,
Do you know how to check if a string contains backslash?
I have a flow action with a REST step and it's working fine but one call encountered an error because the parameter that I'm passing contains a backslash (sample value: abcd\efgh). I tried testing the action by adding another backslash and it ran successfully (sample value: abcd\\efgh).
I wanted to try and check the value of the string and if it has a backslash, I'll add another one before passing the value as parameter to the action. I'm only on the first part (which is to iterate through the characters). I've tried the below but nothing worked.
var testStr = "abcd\efgh";
for (var i = 0; i < testStr.length; i++) {
gs.info(testStr.charAt(i));
}
var testStr = "abcd\efgh";
for (var i = 0; i < testStr.length; i++) {
gs.info(testStr[i]);
}
var testStr = "abcd\efgh";
var testStrArr = testStr.split('');
for (var i = 0; i < testStrArr.length; i++) {
gs.info(testStrArr[i]);
}
Only the characters a-h gets logged. It seems like the backslash is not being read at all. I don't know if I'm missing something here but if you have an idea, please let me know.
Regards,
Nen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2024 03:58 AM
Hi @Nen ,
In your string "abcd\efgh", the sequence \e is not a recognized as escape sequence in JavaScript, so the backslash is ignored, and the string is interpreted as "abcdefg". This is why you are not seeing the backslash in your output.
Please try the below code snippet-
var testStr = "abcd\\efgh"; // Use double backslash to include a literal backslash
for (var i = 0; i < testStr.length; i++) {
gs.info(testStr.charAt(i));
}
var testStr = "abcd\\efgh"; // Use double backslash
for (var i = 0; i < testStr.length; i++) {
gs.info(testStr[i]);
}
var testStr = "abcd\\efgh"; // Use double backslash
var testStrArr = testStr.split('');
for (var i = 0; i < testStrArr.length; i++) {
gs.info(testStrArr[i]);
}
Executed in Background script and got the below output-
Note: Please use gs.print instead of gs.info if you are executing in background script.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2024 05:38 PM
Hi. Thanks for the reply.
"In your string "abcd\efgh", the sequence \e is not a recognized as escape sequence in JavaScript, so the backslash is ignored"
This is exactly my problem that's why I'm trying to find the backslash and add another backslash on the string. The string that I will process is dynamic and is a value on a table record and I just can't manually add the second backslash to it.
Is this something that's not feasible?
