简体   繁体   中英

Javascript string comparison issue with backslash

So I am writing a jquery selector for custom data-xx attribute. What I have as value for this attribute is network path. In my script I am trying to identify the which was clicked by using value of this attribute in my selector

here is code layout

<a data-path="\\network\Dir1\SubDir\SubDir2\file.xml" href="#">Link1</a>

and this is my selector which returns nothing.

$('a[data-path="\\\\network\\Dir1\\SubDir\\SubDir2\\file.xml"]')

only time my selector works is when I just use file name

$('a[data-path*="\file.xml"]')

I am not sure if there is something wrong with the way am escaping backslash here or in the way am using custom attribute selector.

If I do $('a#id').data('path') i get this "\\network\\Dir1\\SubDir\\SubDir2\\file.xml"

thanks

$('a[data-path="\\\\\\\\\\\\\\\\network\\\\\\\\Dir1\\\\\\\\SubDir\\\\\\\\SubDir2\\\\\\\\file.xml"]')

这可以工作,但我不完全知道这是理论。

As stated in your question comments, you need to use 4 backslahes per backslash in the path:

var allLinks = $('a');
var longLink = $('a[data-name="\\\\\\\\network\\\\Dir1\\\\SubDir\\\\SubDir2\\\\file.xml"]');

console.log(" *** links found: ", allLinks.length, longLink.length);

Here is a working example: http://plnkr.co/edit/D2w8G7yTaOusG5qwT51x?p=preview

this is what i did eventually. First comment resolved it for me.

var path = 'value in html';
path.replace(/\\/g, '\\\\');
$(a'[data-path*="' + path+ '"]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM