0

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?

asked Aug 5, 2011 at 5:55
0

4 Answers 4

2

\ \ 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?

answered Aug 5, 2011 at 5:57
Sign up to request clarification or add additional context in comments.

1 Comment

@kevin Peng, what are you trying to achieve?
0

you need to escape the backslash :

t = ("\\\\").replace(/(\\)/g,"");
answered Aug 5, 2011 at 5:59

Comments

0

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,"");
answered Aug 5, 2011 at 5:59

Comments

0

\ 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 \ ...

answered Aug 5, 2011 at 6:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.