-3

For example-

Input- C:\Sumit\dropbox\faces\Monika1\3b590ea4-610a-4c32-8486-bb06bbd23bc5.jpg

Output- \dropbox\faces\Monika1\3b590ea4-610a-4c32-8486-bb06bbd23bc5.jpg

i want to replace C:\Sumit with nothing.

Majid Parvin
  • 4,499
  • 5
  • 29
  • 47
Sumit Sarkar
  • 169
  • 1
  • 2
  • 11

2 Answers2

1

Yes:

var input = "C:\\Sumit\\dropbox\\faces\\Monika1\\3b590ea4-610a-4c32-8486-bb06bbd23bc5.jpg";
var output = input.replace("C:\\Sumit", "");

console.log("Input: " + input);
console.log("Output: " + output);

Notice the double backslash--this is because backslash is an escape character, so the first backslash tells the JavaScript interpreter an escape sequence is happening, and to look at the next character to find out what kind of escape sequence it is. The next character, the second backslash, tells it that the escape sequence produces a literal, single \. In other words, the above "C:\\Sumit" means C:\Sumit when your code is interpreted. Try the "Run code snippet" button and you'll see that the input and output printed on the console have single slashes as they should.

If your input comes from, say, the value of a form element on a web page, then there's no need to do anything special with it: we only escape it here because we're specifying it as a string literal for testing purposes. But the call to input.replace() should still have its backslashes escaped as shown, again because we're using a string literal there.

Kev
  • 15,899
  • 15
  • 79
  • 112
  • I have single slace not double slace – Sumit Sarkar Apr 12 '17 at 17:13
  • @SumitSarkar as I tried to explain, you need to double it in a string literal wherever it would normally occur singly. Otherwise, e.g. in your string the `\S` might get wrongly interpreted as something special. C.f. https://en.wikipedia.org/wiki/String_literal#Escape_sequences – Kev Apr 12 '17 at 20:55
1
var str = "C:\\Sumit\\dropbox\\faces\\Monika1\\3b590ea4-610a-4c32-8486-bb06bbd23bc5.jpg";
var res = str.replace("C:\\Sumit", "");

The result of res will be: \dropbox\faces\Monika1\3b590ea4-610a-4c32-8486-bb06bbd23bc5.jpg

Display name
  • 1,228
  • 1
  • 18
  • 29