0

How to separate a string using '\' in javascript. It automatically converts into a escape sequence. Any help please.

Javascript

var str="file_path1\file_path2\file";
var rev=str.split("\");

This doesn't work.

Fiddle: http://jsfiddle.net/ucw28/

ADDITIONAL DETAILS:

I'm uploading a file using input type= file. I want to obtain the file name without using php. And this problem occurred so I cannot escape the slash in the string itself.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rishi
  • 45
  • 9
  • You've to double the backslash, also in `str`. – Teemu Apr 03 '14 at 17:15
  • possible duplicate of [Escaping backslash in string - javascript](http://stackoverflow.com/questions/8618374/escaping-backslash-in-string-javascript) – Prasanth Apr 03 '14 at 17:18
  • I know that works but as you know I can't alter the string. Anyways thank you. – Rishi Apr 03 '14 at 17:42
  • I found this [question](http://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript). Can someone explain me the top answer in this. Please. – Rishi Apr 03 '14 at 17:54
  • 1
    The regexp in that answer replaces backslashes and slashes with an empty string, not your solution. Which browser actually gives you the full path from file input and how? It supposed to be a fake path anyway... In IE you might get it when running a page locally, but it won't work on [a page loaded from a server](http://jsfiddle.net/URS4e/1/). – Teemu Apr 03 '14 at 18:18
  • Thank you @Teemu.. yes it returns fake path but still the replace code in that answer worked for me. Thank you once again. – Rishi Apr 03 '14 at 19:09

1 Answers1

2

You need to escape the backslashes:

var str="file_path1\\file_path2\\file";
var rev=str.split("\\");
karliwson
  • 3,365
  • 1
  • 24
  • 46