How to use the speciale character '\' in a string in node [duplicate] - javascript

I am passing a path as an argument in a Javascript.
For example I am passing a path as c:\my documents\user\aa.jpg when registering Javascript in client side.
When calling this in a function say, function js(d) then the slash goes missing, hence the value of 'd' becomes c:mydocumentsuseraa.jpg
What to do?

Have you escaped your backslashes ?
alert( 'c:\\my documents\\user\\aa.jpg' );

pass it like this:
c:\\my documents\\user\\aa.jpg
you need to escape the slash char. thanks
You should always pass the slash char
which has special meaning for example,
you can use it to specify new lines
like \n, tabs \t, etc. So you should
escape it with another slash char to
make it come single slash char.

You said when registering javascript in client side.
I assume you are doing this in the code behind as you have tagged asp.net.
So I think you would need to use # before the string while registering the scipt.
Like #"c:\abc\xyz.jpg"
OR you can go by the method Sarfraz mentiond. i.e. pass the string as "c:\\abc\\xyz.jpg"
I hope this helps.

Related

extract last portion from url

Having the following link:
/C:\Users\xxx\Desktop\yyy\public\assets\1634648850202.jpg
How do i extract 1634648850202.jpg?
i tried:
const lastplace = thisUrl.substring(thisUrl.lastIndexOf("\"));
This does not work, because the backslash is recognized as a functional character (is this the right term?)
Here are 2 Questions from my side:
how do i extract the last part?
how should i handle backslashes in general?
Common way:
thisUrl.split(/\/|\\/).pop() // 1634648850202.jpg
I also recommend to split url/path by both slashes (/,\) 'cause of different enviromnents/systems uses diffrent character.
UPD: just make your \ double to handle it (you're right, it's a escape character), like this:
''.lastIndexOf('\\')
Also you need do the same when you initiate your string:
const thisUrl = '/C:\\Users\\xxx\\Desktop\\yyy\\public\\assets\\1634648850202.jpg';

Only one backslash in javascript

I know that backslash is escape character in js. And to display one backslash, I need to write two.
But I am having express server that send request to database and here I need to add only one backslash. SO if filter contains two backslashes, replace it only with one. How to write it?
filter.replace("\\", String.fromCharCode(92)); //do two not one
filter.replace("\\", "\"); //doesn't work, syntax error
example
"aaaa\\aaaa" - > "aaaa\aaaa"
Another ideas?
---UPADTE---
The string that is send to database contains two backslashes but js GUI shows only one (of course).
How to write it?
You said it yourself:
to display one backslash, I need to write two.
So, if you have two in the string to start with, then you need to replace two (type four) with one (type two).
var filter = "This string has a double slash in it: \\\\";
console.log(`The original string: ${filter}`);
filter = filter.replace("\\\\", "\\");
console.log(`The filtered string: ${filter}`);
Quentin answered your question, but another way to think about it is that two backslashes written into a sting will resolve to a single backslash as soon as you do anything with it.
For example:
Console.Log("\");
//Returns Error
Console.Log("\\");
//Returns: \
var i = "this is a backslash \\"
//i now contains only one backslash
Console.Log(i);
//Returns: this is a backslash \
Edit:
Since you clarified that it's after this in the querying that it gets messed up, you could try making sure you've assigned it to a variable and then passed it to the query.
i = "A string containing backslashes \\"
sql.Query(i);
Edit 2:
Oh, I just got it, you're trying to escape colons ':' which is already handled in JS. So if the query isn't parsing your escape characters than just \: should be perfectly valid.

How to split a string in javascript by special char \

I believe that this is simple and I'm missing something. I want to split a physical path in windows with javascript. So I try with String#split function, but my result was inespected.
For this string
"C:\CLC\VIDA\Web\_REPOSITORIO\Colectivos\ReembolsosWeb\TMP_011906169_01_01.pdf"
I'm getting this result
var test = "C:\CLC\VIDA\Web\_REPOSITORIO\Colectivos\ReembolsosWeb\TMP_011906169_01_01.pdf";
test.split("\"); //throws error
test.split("\\"); //result in -> ["C:CLCVIDAWeb_REPOSITORIOColectivosReembolsosWebTMP_011906169_01_01.pdf"]
test.split(/\\/); // -> the regex is the same as above
One last thing, in my test, I found that to get the result that I want I could do it like this
var test2 = "C:\\CLC\\VIDA\\Web\\_REPOSITORIO\\Colectivos\\ReembolsosWeb\\TMP_011906169_01_01.pdf"
test2.split("\\"); // -> ["C:", "CLC", "VIDA", "Web", "_REPOSITORIO", "Colectivos", "ReembolsosWeb", "TMP_011906169_01_01.pdf"]
So my question is, how can I split the string from test var to get the array from the last case?
Strings in javascript support escape sequences via the backslash (\). For example if you need a tab in your string you can add a \t anywhere in your string and it will be replaced with a tab, a \n will be replaced with a new line.
The backslashes in test are either converted to their respective characters or dropped because they are invalid escape sequences.
To get around this you can escape one backslash with another to get a single normal backslash. The downside is that this cannot be done in javascript. Generally I paste my string in to notepad/N++/Code/Sublime and replace all \ with \\
Since you are hard coding the string you need to escape all backslashes. After that you can use test.split("\\") which, itself contains an escaped backslash.
So, as far as Javascript is concerned, your code looks like this.
var test = "C:CLCVIDAWeb_REPOSITORIOColectivosReembolsosWebTMP_011906169_01_01.pdf";
To make javascript see the string correctly you need to make it look like this...
var test = "C:\\CLC\\VIDA\\Web\\_REPOSITORIO\\Colectivos\\ReembolsosWeb\\TMP_011906169_01_01.pdf";
Firstly, note that when you have a single backslash in a string, it is used for escaping the next character. It is just ignored if there is no special character next to it to escape.
Now, just have a look at your string :
var test = "C:\CLC\VIDA\Web\_REPOSITORIO\Colectivos\ReembolsosWeb\TMP_011906169_01_01.pdf"
Don't you think all of your single backslashes will be ignored here?
So, the solution is simple, what you have already tried successfully. To escape all your backslashes with another backslash.
var test2 = "C:\\CLC\\VIDA\\Web\\_REPOSITORIO\\Colectivos\\ReembolsosWeb\\TMP_011906169_01_01.pdf"
test2.split("\\"); // -> ["C:", "CLC", "VIDA", "Web", "_REPOSITORIO", "Colectivos", "ReembolsosWeb", "TMP_011906169_01_01.pdf"]
But, are you worried about any dynamic data which has such backslash? (For example, coming from a text input or a file input.) Don't think about escaping the backslash inside it. Because you don't need to do that! It's already a well formatted string for you, which you can use as it is. You need to escape only when you are hard coding the string yourself.

JS - RegExp for detecting ".-" , "-."

I am bit confused with the RegExp I should be using to detect ".-", "-." it indeed passes this combinations as valid but in the same time, "-_","_-" get validated as well. Am I missing something or not escaping something properly?
var reg=new RegExp("(\.\-)|(\-\.)");
Actually seems any combination containing '-' gets passed. it
Got it thank you everyone.
You need to use
"(\\.-)|(-\\.)"
Since you're using a string with the RegExp constructor rather than /, you need to escape twice.
>>> "asd_-ads".search("(\.\-)|(\-\.)")
3
>>> "asd_-ads".search(/(\.\-)|(\-\.)/)
-1
>>> "asd_-ads".search(new RegExp('(\\.\-)|(\-\\.)'))
-1
In notation /(\.\-)|(\-\.)/, the expression would be right.
In the notation you chose, you must double all backslashes, because it still has a special meaning of itself, like \\, \n and so on.
Note there is no need to escape the dash here: var reg = new RegExp("(\\.-)|(-\\.)");
If you don't need to differentiate the matches, you can use a single enclosing capture, or none at all if you only want to check the match: "\\.-|-\\." is still valid.
You are using double quotes so the . doesn't get escaped with one backslash, use this notation:
var reg = /(\.\-)|(\-\.)/;

Javascript slash is missing when called in argument

I am passing a path as an argument in a Javascript.
For example I am passing a path as c:\my documents\user\aa.jpg when registering Javascript in client side.
When calling this in a function say, function js(d) then the slash goes missing, hence the value of 'd' becomes c:mydocumentsuseraa.jpg
What to do?
Have you escaped your backslashes ?
alert( 'c:\\my documents\\user\\aa.jpg' );
pass it like this:
c:\\my documents\\user\\aa.jpg
you need to escape the slash char. thanks
You should always pass the slash char
which has special meaning for example,
you can use it to specify new lines
like \n, tabs \t, etc. So you should
escape it with another slash char to
make it come single slash char.
You said when registering javascript in client side.
I assume you are doing this in the code behind as you have tagged asp.net.
So I think you would need to use # before the string while registering the scipt.
Like #"c:\abc\xyz.jpg"
OR you can go by the method Sarfraz mentiond. i.e. pass the string as "c:\\abc\\xyz.jpg"
I hope this helps.

Categories

Resources