Javascript slash is missing when called in argument - 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

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

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.

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';

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 = /(\.\-)|(\-\.)/;

Building a Hashtag in Javascript without matching Anchor Names, BBCode or Escaped Characters

I would like to convert any instances of a hashtag in a String into a linked URL:
#hashtag -> should have "#hashtag" linked.
This is a #hashtag -> should have "#hashtag" linked.
This is a [url=http://www.mysite.com/#name]named anchor[/url] -> should not be linked.
This isn't a pretty way to use quotes -> should not be linked.
Here is my current code:
String.prototype.parseHashtag = function() {
return this.replace(/[^&][#]+[A-Za-z0-9-_]+(?!])/, function(t) {
var tag = t.replace("#","")
return t.link("http://www.mysite.com/tag/"+tag);
});
};
Currently, this appears to fix escaped characters (by excluding matches with the amperstand), handles named anchors, but it doesn't link the #hashtag if it's the first thing in the message, and it seems to grab include the 1-2 characters prior to the "#" in the link.
Halp!
How about the following:
/(^|[^&])#([A-Za-z0-9_-]+)(?![A-Za-z0-9_\]-])/g
matches the hashtags in your example. Since JavaScript doesn't support lookbehind, it tries to either match the start of the string or any character except & before the hashtag. It captures the latter so it can later be replaced. It also captures the name of the hashtag.
So, for example:
subject.replace(/(^|[^&])#([A-Za-z0-9_-]+)(?![A-Za-z0-9_\]-])/g, "$1http://www.mysite.com/tag/$2");
will transform
#hashtag
This is a #hashtag and this one #too.
This is a [url=http://www.mysite.com/#name]named anchor[/url]
This isn't a pretty way to use quotes
into
http://www.mysite.com/tag/hashtag
This is a http://www.mysite.com/tag/hashtag and this one http://www.mysite.com/tag/too.
This is a [url=http://www.mysite.com/#name]named anchor[/url]
This isn't a pretty way to use quotes
This probably isn't what t.link() (which I don't know) would have returned, but I hope it's a good starting point.
There is an open-source Ruby gem to do this sort of thing (hashtags and #usernames) called twitter-text. You might get some ideas and regexes from that, or try out this JavaScript port.
Using the JavaScript port, you'll want to just do:
var linked = TwitterText.auto_link_hashtags(text, {hashtag_url_base: "http://www.mysite.come/tag/"});
Tim, your solution was almost perfect. Here's what I ended up using:
subject.replace(/(^| )#([A-Za-z0-9_-]+)(?![A-Za-z0-9_\]-])/g, "$1#$2");
The only change is the first conditional, changed it to match the beginning of the string or a space character. (I tried \s, but that didn't work at all.)

Categories

Resources