JavaScript backslash (\) in variables is causing an error -


in javascript, when put backslash in variables like:

var ttt = "aa ///\\\"; var ttt = "aa ///\";  

javscript shows error.

if try restrict user in entering character, error:

(("aaa ///\\\").indexof('"') != -1) 

restricting backslashes user input not strategy, because have show annoying message user.

why getting error backslash?

the backslash (\) escape character in javascript (along lot of other c-like languages). means when javascript encounters backslash, tries escape following character. instance, \n newline character (rather backslash followed letter n).

in order output literal backslash, need escape it. means \\ output single backslash (and \\\\ output two, , on). reason "aa ///\" doesn't work because backslash escapes " (which print literal quote), , string not terminated. similarly, "aa ///\\\" won't work, because last backslash again escapes quote.

just remember, each backslash want output, need give javascript two.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -