Escaping backslash in string - javascript
For security reasons, it is not possible to get the real, full path of a file, referred through an <input type="file" />
element.
This question already mentions, and links to other Stack Overflow questions regarding this topic.
Previous answer, kept as a reference for future visitors who reach this page through the title, tags and question.
The backslash has to be escaped.
string = string.split("\\");
In JavaScript, the backslash is used to escape special characters, such as newlines (\n
). If you want to use a literal backslash, a double backslash has to be used.So, if you want to match two backslashes, four backslashes has to be used. For example,alert("\\\\")
will show a dialog containing two backslashes.
Escaping backslash in a string containing backslash
Looks like there's no other way other than eval (JSON.parse doesn't like new lines in strings)
NOTE: The function would return false if it has a trailing backslash
function backslashString(input){
input = input.replace(/`/g, '\\`'); // Escape quotes for input to eval
try{
return eval('`'+input+'`');
}catch(e){ // Will return false if input has errors in backslashing
return false;
}
}
escaped backslash coming out as \\ in string
console.log
functions give you debugging information. Different implementations of console.log
are different. There is no standard which describes how they should represent a string.
Often they provide representations of strings that include escape characters in the visible output.
If you want to see the processed value of the string in Node, then write to STDOUT.
process.stdout.write("Your \\ string");
JavaScript backslash (\) in variables is causing an error
The backslash (\
) is an escape character in Javascript (along with a lot of other C-like languages). This means that when Javascript encounters a backslash, it tries to escape the following character. For instance, \n
is a newline character (rather than a backslash followed by the letter n).
In order to output a literal backslash, you need to escape it. That means \\
will output a single backslash (and \\\\
will output two, and so on). The reason "aa ///\"
doesn't work is because the backslash escapes the "
(which will print a literal quote), and thus your string is not properly terminated. Similarly, "aa ///\\\"
won't work, because the last backslash again escapes the quote.
Just remember, for each backslash you want to output, you need to give Javascript two.
How to escape backslash in JavaScript?
open console and type
'\'.replace(/\\/g, '\');
fails because the slash in the string isn't really in the string, it's escaping ''\\'.replace(/\\/g, '\');
works because it takes one slash and finds it.your regex works.
Escaping Backslash in a RegExp String
Because you're creating the RegEx from a string literal, the double backslash is being interpreted as an escaped backslash within the string and you're winding up with this RegEx:
^[\w., #&/-]+$
This matches word characters, but backslashes are nowhere to be found.The solution: escape both backslashes, and add one for the \w
, resulting in six backslashes:
var regex = new RegExp("^[\\\\\\w., #&/-]+$");
Or even better, use a regular expression literal and only use three backslashes:var regex = /^[\\\w., #&/-]+$/;
How to escape backslashes from a string in javascript
Escape the backslashes by doubling them.
// The json would be something like this.
data = [
{"name": "Something", "link": "C:\\something\\something"},
{"name": "Something Else", "link": "C:\\something\\something_else"}
];
// Loop over the json and add the elements to the html afterwards
var list = '';
$.each(data, function () {
list += `
<p> ${this.name} </p>
<input class="form-control" type="text" value="${this.link}">
`;
});
$(".some-container").html(list);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="some-container"></div>
Related Topics
Why Are Es6 Classes Not Hoisted
Attaching Click Event to a Jquery Object Not Yet Added to the Dom
JavaScript String Array to Object
JavaScript Ternary Operator Example with Functions
Conversion Between Utf-8 Arraybuffer and String
How to Use Local Storage in Angular
Resize Jqgrid When Browser Is Resized
How to Detect Browser Window /Tab Close Event
What's the API Key for in Google Maps API V3
Find the Exact Height and Width of the Viewport in a Cross-Browser Way (No Prototype/Jquery)
Cancel/Kill Window.Settimeout() Before It Happens
Return Index of Greatest Value in an Array
Counting Frequency of Characters in a String Using JavaScript
Why Does If("String") Evaluate "String" as True But If ("String"==True) Does Not
Add Two Functions to Window.Onload
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error
How to Upload Preview Image Before Upload Through JavaScript