admin管理员组文章数量:1279057
I have an application which parses strings and passes them via jQuery to a popup window. Some of the strings I am passing contain a field for web addresses OR internal C: drive locations. When I pass the string to the window, the backslash ("\") from the windows path is being stripped out (the web addresses using the forwardslash is, of course, no problem. I am trying to pass these strings as links, so the user can just click on the link and off they go. Obviously, if the backslashes are stripped, the link does not work. How the heck do I test for this? I have tried a few simple regexes that don't work.
EDIT: The strings that I am parsing e from a Sharepoint list of applications, and I have no control over their format, they are simply entered into the list as "\drive\then\the\path\to\the\file". My understanding is that I need to check to see if there is a backslash, then escape it by adding another backslash. But how?
EDIT 2: Good to know that I was on the right track. From what I had read on the internets, I had figured it was something along this line. However, I still can't get this to work. Maybe you folks could help me out....
I have grabbed the field from the sharepoint list like this (sharepoint stores field names with a prefix of 'ows_':
var devPath = "<a class='h_link' href='"+$(this).attr('ows_DEVPathURLServer')+"'>"+$(this).attr('ows_DEVPathURLServer')+"</a>";
This works great for web URLs, as stated.
So then I messed around with doing a .replace(/\/g,'\\');, but it won't run. So then I thought I would try:
var unregged = $(this).attr('ows_DEVPathURLServer');
var regged = unregged.toString().split('\\').join('\\\\');
alert(regged);
var devPath = "<a class='h_link' href='"+regged+"'>"+regged+"</a>";
Just to break down my thought process. Doesn't work. What am I missing?
Thanks!
I have an application which parses strings and passes them via jQuery to a popup window. Some of the strings I am passing contain a field for web addresses OR internal C: drive locations. When I pass the string to the window, the backslash ("\") from the windows path is being stripped out (the web addresses using the forwardslash is, of course, no problem. I am trying to pass these strings as links, so the user can just click on the link and off they go. Obviously, if the backslashes are stripped, the link does not work. How the heck do I test for this? I have tried a few simple regexes that don't work.
EDIT: The strings that I am parsing e from a Sharepoint list of applications, and I have no control over their format, they are simply entered into the list as "\drive\then\the\path\to\the\file". My understanding is that I need to check to see if there is a backslash, then escape it by adding another backslash. But how?
EDIT 2: Good to know that I was on the right track. From what I had read on the internets, I had figured it was something along this line. However, I still can't get this to work. Maybe you folks could help me out....
I have grabbed the field from the sharepoint list like this (sharepoint stores field names with a prefix of 'ows_':
var devPath = "<a class='h_link' href='"+$(this).attr('ows_DEVPathURLServer')+"'>"+$(this).attr('ows_DEVPathURLServer')+"</a>";
This works great for web URLs, as stated.
So then I messed around with doing a .replace(/\/g,'\\');, but it won't run. So then I thought I would try:
var unregged = $(this).attr('ows_DEVPathURLServer');
var regged = unregged.toString().split('\\').join('\\\\');
alert(regged);
var devPath = "<a class='h_link' href='"+regged+"'>"+regged+"</a>";
Just to break down my thought process. Doesn't work. What am I missing?
Thanks!
Share Improve this question edited Mar 29, 2012 at 14:00 mrwienerdog asked Mar 29, 2012 at 13:13 mrwienerdogmrwienerdog 8253 gold badges18 silver badges35 bronze badges 3- You could url encode (window.encodeURIComponent) the paths before you pass them. – reedlauber Commented Mar 29, 2012 at 13:18
- As a fairly big newb (read 'student'), I am unfamiliar with this. How would I go about this? – mrwienerdog Commented Mar 29, 2012 at 13:22
- Consider changing title of question (and maybe tags too) to represent what question is about. Don't really see how it's related to "regex troubles" – Juicy Scripter Commented Mar 29, 2012 at 13:48
4 Answers
Reset to default 3In order to "double" each backslash you can do:
myString.replace(/\\/g, '\\\\')
Please note than each time you see two backslash in the code, it's actually one since we need to escape them in our code as well.
the g
means Global replacement, so if you have several \ they will all be replaced.
Here's a faster, regex-less, one line solution:
myString.split('\\').join('\\\\');//replaces all backslashes with double backslashes.
Using regexp here is just overkill...
You can simply check if this is location that is "local" and convert it to File URI scheme prior to passing it further.
Something like this may do the trick:
var localPathRegex = /^\\(\w)\\/i;
if (localPathRegex.test(url)){
url = url.replace(localPathRegex, 'file:///$1:/').replace(/\\/g, '/');
}
This should convert \d\then\the\path\to\the\file
to file:///d:/then/the/path/to/the/file
You need to escape your backslashes with another backslash. Your passed url would look something like this:
c:\\mydrive\\MyFolder
本文标签: javascriptproblems retaining original backslashes when passing url Stack Overflow
版权声明:本文标题:javascript - problems retaining original backslashes when passing url- Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266053a2368483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论