We are passing following view bag value;
ViewBag.Alphabets = "a\\b\\c";
We are retrieving this value in JavaScript in Razor view;
console.log('@ViewBag.Alphabets');
JS Output
console.log('a\b\c');
Console Output
a_c
Where did the “\” goes? Since this is a part of escape characters so JS automatically removes “\\” and replaced it with “_”. “\b” is a metacharacter so it’s gone as well.
We can fix it by using Replace command on ViewBag.
console.log('@ViewBag.Alphabets.Replace("\\", "\\\\")');
JS Output
console.log('a\\b\\c;');
Console Output
a\b\c
References
https://www.tutorialspoint.com/escape-characters-in-javascript
Add to favorites