tags:

views:

140

answers:

5

when I put slash to some variables value like

"aa ///\\\" or "aa ///\"

then javscript shows error.

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

the input is not in my hand so, i can't add slashes accordingly before assigning it to variable.

I used this and this

But none of this works for me. If i want to restrict user to give this character then i also got an error

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

and restricting is not a good way because you had to show a message to user that user can't give a name which has (\) character.

+1  A: 

You have to escape each \ to be \\:

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

Updated: I think this question is not about the escape character in string at all. The asker doesn't seem to explain the problem correctly.

because you had to show a message to user that user can't give a name which has (\) character.

I think the scenario is like:

var user_input_name = document.getElementById('the_name').value;

Then the asker wants to check if user_input_name contains any [\]. If so, then alert the user.

If user enters [aa ///\] in HTML input box, then if you alert(user_input_name), you will see [aaa ///\]. You don't need to escape, i.e. replace [\] to be [\\] in JavaScript code. When you do escaping, that is because you are trying to make of a string which contain special characters in JavaScript source code. If you don't do it, it won't be parsed correct. Since you already get a string, you don't need to pass it into an escaping function. If you do so, I am guessing you are generating another JavaScript code from a JavaScript code, but it's not the case here.

I am guessing asker wants to simulate the input, so we can understand the problem. Unfortunately, asker doesn't understand JavaScript well. Therefore, a syntax error code being supplied to us:

var ttt = "aa ///\";

Hence, we assume the asker having problem with escaping.

If you want to simulate, you code must be valid at first place.

var ttt = "aa ///\\"; // <- This is correct
// var ttt = "aa ///\"; // <- This is not.

alert(ttt); // You will see [aa ///\] in dialog, which is what you expect, right?

Now, you only need to do is

var user_input_name = document.getElementById('the_name').value;
if (user_input_name.indexOf("\\") >= 0) { // There is a [\] in the string
  alert("\\ is not allowed to be used!"); // User reads [\ is not allowed to be used]
  do_something_else();
  }

Edit: I used [] to quote text to be shown, so it would be less confused than using "".

livibetter
Imrul
Are you coding in PHP in server-side? Because `var t` doesn't seem like a PHP variable declaration.
livibetter
i am using javascript to solve this. I tried to use escape(string) and unescape(string). But these didn't work either
Imrul
Then, my answer already told you how to escape \ when you input a string in JS code. The `escape()` is not for that purpose.
livibetter
i got u'r answer. But that is not what i want. if your input is dynamic then how can you put an extra slash in your string. U need a function to do that. I used some function as i mentioned earlier, but i didn't got what i want.
Imrul
@Imrul I updated my answer, please see if it fits your problem.
livibetter
A: 

The jsfiddle link to where i tried out your query http://jsfiddle.net/A8Dnv/1/ its working fine @Imrul as mentioned you are using C# on server side and you dont mind that either: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx

jknair
HTML is not JavaScript.
Gumbo
@gumbo and your point being ??? btw imrul stated "yes, it's in user input from HTML input filed in Browser" would be helpful if you could elaborate thanks
jknair
+5  A: 

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.

Daniel Vandersluis
I think you confused `//` with `\\‍`.
Gumbo
Oops, that's what happens when I write answers at 1:15am ;) Fixed.
Daniel Vandersluis
Great explanation. I give 1 up vote for this. But i didn't accept this because i didn't get the answer. The input is in User's hand and if the user try to input this kind of sequence then is it possible to overcome this.
Imrul
A: 

You may want to try the following, which is more or less the standard way to escape user input:

function stringEscape(s) {
    return s ? s.replace(/\\/g,'\\\\').replace(/\n/g,'\\n').replace(/\t/g,'\\t').replace(/\v/g,'\\v').replace(/'/g,"\\'").replace(/"/g,'\\"').replace(/[\x00-\x1F\x80-\x9F]/g,hex) : s;
    function hex(c) { var v = '0'+c.charCodeAt(0).toString(16); return '\\x'+v.substr(v.length-2); }
}

This replaces all backslashes with an escaped backslash, and then proceeds to escape other non-printable characters to their escaped form. It also escapes single and double quotes, so you can use the output as a string constructor even in eval (which is a bad idea by itself, considering that you are using user input). But in any case, it should do the job you want.

HTH,

Roy

Roy Sharon
<script type="text/javascript"> var ttt = stringEscape("aa ///\\\");</script> Please run this after you add your function in HTML HEAD. You will see the difference.
Imrul
@Imrul, not sure I understand. Writing a *script* using "aa ///\\\" is not the same as getting "aa ///\\\" as input from the user. In the former case, the triple backslash and the double quotes following them are interpreted as a single backslash followed by a double quotes. In the later case the input is interpreted verbatim, and contains the three backslashes. The stringEscape() function converts a user input into a string literal in script form. I thought that this is what you were trying to achieve, no?
Roy Sharon
A: 

If you want to use special character in javascript variable value, Escape Character (\) is required.

Backslash in your example is special character, too.

So you should do something like this,

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

or

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

But Escape Character not require for user input.

When you press / in prompt box or input field then submit, that means single /.

diewland