最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to separate "t" to char '' + 't' - Stack Overflow

programmeradmin0浏览0评论

I have got string containing windows path to file. It looks something like this:

var path = 'c:\test\old\new\ring.txt';

Os and browser gave it to me. So I can not change it by manually writing '\' in front of every '\'. Every '\' is just '\,' regular win path delimiter. Problem arises if '\' is followed by t, r, n.

Then it bees white space. But if I pass that as function parameter string is automatically changed in way that all \ are gone!

So, is there a way to somehow separate '\t' '\n' '\r'... to '\' + char?

If not, is is possible to somehow escape '\' so that they remain '\'? Like \ and not to bee tab, new line, etc

edit: Maybe I wasn't clear enough.

I have got string containing windows path to file. It looks something like this:

var path = 'c:\test\old\new\ring.txt';

Os and browser gave it to me. So I can not change it by manually writing '\' in front of every '\'. Every '\' is just '\,' regular win path delimiter. Problem arises if '\' is followed by t, r, n.

Then it bees white space. But if I pass that as function parameter string is automatically changed in way that all \ are gone!

So, is there a way to somehow separate '\t' '\n' '\r'... to '\' + char?

If not, is is possible to somehow escape '\' so that they remain '\'? Like \ and not to bee tab, new line, etc

edit: Maybe I wasn't clear enough.

Share Improve this question edited Oct 4, 2011 at 15:20 CoR asked Oct 4, 2011 at 14:20 CoRCoR 3,9145 gold badges37 silver badges42 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

Escape the backslash by doubling it: c:\\test\\etc.

var path = 'c:\\test\\old\\new\\ring.txt';

The backslash is the escape character, so you are escaping the t, n, o, and r. You need to escape the backslash. You can do that like this:

var path = 'c:\\test\\old\\new\\ring.txt'; 

If you really need to use backslashes, escape them: 'C:\\whatever'

If the path is actually meant to be used: Use forward slashes. While windows displays backslashes, it accepts forward slashes, too.

Sorry, it was my mistake. OS already gave me escaped backslashes '\' path! I was getting undefined from mu function because of this pointing to html object and not js object with function.

My bad :(

发布评论

评论列表(0)

  1. 暂无评论