regexp: var s = document.getElementById("username").value; if s == "\ \ \ \"; var result = s.replace(/\/g,"") will be wrong? why that firebug error ?
hope to the result is equals == "", but firebug is output:
SyntaxError { source="with(_FirebugCommandLine){("\ \").replace(/(\)/g,"");\n};", message="unterminated string literal", fileName="resource://firebug_rjs/console/commandLineExposed.js", more...}
why that? please help me?
4 Answers 4
\ \ is a special character. See Special Characters in Javascript.
You have to escape \.
("\\ \\").replace(/(\\)/g,"");
Should work.
BTW, what are you trying to do in your regEx match?
1 Comment
you need to escape the backslash :
t = ("\\\\").replace(/(\\)/g,"");
Comments
Because you haven't escaped the backslashes in the string.
The backslash before the ending quote means that the quote is part of the string instead, so the string doesn't end until the next quote, so your code contains:
(- a string with the content
").replace(/(\\)/g, - a string with the content
);that is missing the ending quote
Escape the backslashes by doubling them:
("\\ \\").replace(/(\\)/g,"");
Comments
\ symbol is regular expression...
\n = newline;
\t = tabspace;
\\ = "\" symbol;
so if you write // its will be mark as comment....
if you write \/ = "/" string..
but now you write "....replace(/\/gi)"...\/=/ will mark as string, so next word cannot close.. you must
var s = document.getElementById("username").value;
if(s.search("\ \ \ \")>=0){
s.replace(/\\/g,"");
}
the \\ meaning \ ...