2

Here are the test cases

  • \\192.168.100.1\foldername\filename
  • actividirectoryName\username
  • somethingelse\text\\and this
  • \

How do I escape these strings and again un-escape it.

I tried something like this "mystring\something".replace(/[\\]/g, '\\\\'); but it do sent work right when I have string like "name\b" or "text\n" or "text\t"

Since those are reserved keys to tab, newline and other. What is the best way to handle this

Update: even here in this edit i had to write 3 back slashes to show you two back slashes? when I first wrote 2 back slashes, it was showing only 1 back slash

HaBo
  • 13,999
  • 36
  • 114
  • 206
  • 1
    Why do you need to escape them? If you're putting them in a URL, try `encodeURIComponent()` – Rory McCrossan Apr 12 '16 at 13:13
  • not in URL. this is the text added in textarea field in a web form. And how do i decode it back. "string\b" id not work – HaBo Apr 12 '16 at 13:20
  • http://stackoverflow.com/questions/6417641/converting-backslashes-into-forward-slashes-using-javascript-does-not-work-prope – Harpreet Singh Apr 12 '16 at 13:28
  • @HarpreetSingh i don't want to convert those to something. I want to escape those and use it. and again un-escape those – HaBo Apr 12 '16 at 14:04
  • I think you need to create a proper test case. Write [a snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/). Use a textarea in your demo since you are using a textarea in the real code. Explain why you need to escape the data (at the moment you only say you are unescaping it, so there must be an important middle step we are missing). – Quentin Apr 12 '16 at 15:08

1 Answers1

0

Depending on how you want to solve this depends on where these strings are stored.

There are only two ways of handling this, both of which require storing the variables else where before using them.

If you are using php you can use Raw. If you are doing ajax requests or loading values into the DOM, you can use javascript to get those values, and they will be held with the baskslashes.

var text = document.getElementById('id');
var a = document.getElementById('v').value;
var b = String.raw `${a}`
var c = String.raw `\\192.168.100.1\foldername\filename`
var d = '\\192.168.100.1\foldername\filename'
var test = [
  d + ' - in JavaScript',
  c + ' - in raw',
  a + ' - in HTML',
  b + ' - in raw variable'
];
for (var i = 0, l = test.length; i < l; i++) {
  text.value += test[i] + '\n';
}
textarea {
  width: 100%;
}
<input id='v' value='\\192.168.100.1\foldername\filename' type='hidden' />
<textarea rows='10' id='id'></textarea>

To use php and raw it would look something like this

String.raw`<?=$variable?>`

EDIT Once you have this raw data you can start using REGEX and other escaping means on it.

var c = String.raw `\\192.168.100.1\foldername\filename<\/p>`
var text2 = document.getElementById('id2');
text2.value = (c + '').replace('\\\\', '\\').replace('\\/', '/');
textarea {
  width: 100%;
}
<textarea rows='10' id='id2'></textarea>
Roger
  • 3,226
  • 1
  • 22
  • 38
  • Thank you for this, i got a extended question on this. How do I replace <\/span><\/p> like and domain\\user\'s like domain\user\'s – HaBo Apr 12 '16 at 17:58
  • replace('\\\\', '\\').replace('\\/', '/'); this will only replace the first occurrence, not all occurrences – HaBo Apr 12 '16 at 18:45
  • finally got it worked with little more changes to what you gave – HaBo Apr 12 '16 at 19:42