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.
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.
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.
Javascript how to verify if string contain ' \ ' with includes()
Because it's an escape character, you have to escape it with another backslash. :)
if (this.inputCity.includes('\\')){
// do something,,,
}
Set value with backslash to input in JQuery
Escape the \
character like this: $('#some_id').val('someval \\')
$('#some_id').val('someval \\')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input id = 'some_id' type='text'/>
Related Topics
Validation Using Yup to Check String or Number Length
Regular Expression Which Allow Only Characters or Space
How to Import Modules from All Files in a Directory, Using a Wildcard
How to Get the Contextpath from Javascript, the Right Way
Disable Click Outside of Bootstrap Modal Area to Close Modal
Howto Trigger Input Event Programmatically on Input Text Field
How to Mock the JavaScript Window Object Using Jest
Javascript Innerhtml Not Updating Element
Reactjs: Uncaught Referenceerror: Require Is Not Defined
How to Validate Mobile Number for Countries in Angular
Typescript - Possible to Disable Type Checking
Preventing Page from Scrolling on Focus Switching
How to Write an Onclick Function for a Dynamically Generated Button
How to Redirect Url After Login Successful Via Ajax Call in PHP
Updating Array in Object Properties Using Property'S Name
Vuejs Set Active Class, When One Li Element Clicked in V-For Loop