views:

71

answers:

3

Hi. I have to check a string client side. If it contain only "\" (1, 2, or 1000 time) i need to refuse it. Code (with some of your suggestion that unfortunatly dont work):

value.replace(/\\/g, '');
if(value=="") alert("NO"); else alert("YES");

so :

value="\" = NO
value="\hello \people" = YES
value="\\\\hello \\people" = YES
value="hello \\people" = YES
value="hello people\" = YES
value="\\" = NO
value="\\\\\\" = NO
value="\\ \\\\ \ \\\" = NO
+3  A: 

If the string has actual single backslashes (as in, not backslashes at all but the escape characters \a and \F in your example) then you're going to have a tough time, however if they are properly escaped you can simply do:

str = str.replace( /\\/g, '' )

to remove them.

thenduks
uhm. In fact i can have string like "hey \\\\\string \\hey \hey" and i have to remove all "\" not the first one. I try string.replace('\\',''); but it doesnt work...
markzzz
And in fact with \ or many \ this method doesnt work... :(
markzzz
You need the `g` modifier on the regex... It can't be just a string. `/\\/g` not `'\\'`
thenduks
@markzzz: Look at thenduks' answer again. There's a big difference between `/\\/` and `/\\/g`.
T.J. Crowder
uhm yes, but it also doesnt work on my problem... :(
markzzz
@markzzz: FYI, in English you're coming across confrontational and not like you appreciate thenduks taking the time to try to help you. I expect it's just a language thing, thought you should know.
T.J. Crowder
oh no sorry! of course should be my english fault. I really appreciate your helps dudes hehe! sorry, i don't want offend you!
markzzz
@markzzz: No worries, just for future reference... :-)
T.J. Crowder
So markzzz, what doesn't work about it? It works fine for me in testing. Open up Firebug and write: `var str = "hello\\there's backslashes\\man!"; alert( str.replace(/\\/g, '') );` and you should get `hellothere'sbackslashesman!` in an alert box. If not, what happens instead?
thenduks
I think I see your problem. You're assuming that `str.replace(...)` _modifies the original string_ but it definitely will not. In your updated question, you need to do something like `value = value.replace(...)`
thenduks
+2  A: 
T.J. Crowder
uhm no i tried. If the string is only "\" or "\\" it doesnt work...
markzzz
@markzzz: It **does** work: http://jsbin.com/ukepo3
T.J. Crowder
A: 

In Javascript:

myvar=myvar.replace(/\\/g,'');

In PHP:

$myvar=str_replace('\\','',$myvar);

Note both of them should be given a double back-slash, as it is an escape character.

It's fine to do it in Javascript, but you'll defintely need to do it server-side as well if you want to be sure, as a browser user could disable javascript if they want to get around your validation. (you didn't specify a server-side language here, but I assumed PHP as you've asked about it before)

You didn't say the reason why you don't want it to get to your database? If you're trying to prevent hacking, you'll need to do more than this as it's not just back-slashes that can do damage.

Spudley
No, `myvar=myvar.replace('\\','');` will only replace the first backslash.
T.J. Crowder
i also tried your code. but if i put value as "\\" or "\" or "\\\\" the script say YES and i don't want this :)
markzzz
@TJ: Oops -- thanks. edited to add the g modifier.
Spudley
@markzzz: when you say "the script say YES", do you mean on the last one of your examples? That one will say 'yes', because it will still contain spaces, even after you've removed the slashes. Perhaps you also want to trim loose spaces from it? Use JQuery $.trim(myvar); or $myvar=trim($myvar); in PHP.
Spudley
i remove each time the \ slash for my string. That's because i send the var to the server, and if somethings fail i resend the value to the client (whose return to the input text for example). The problem is that i need to escape them, otherwise they fail (just think to put a value like this ' hey how"are you'? into a input box like <input type="text" value="<?=var?>". It fails. So i escape with "\". But if i found \ in the var, it add more \ (and that's horrible). Is it clear more or less hehe. Sorry my english is so crap :)
markzzz