0

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
7
  • 10
    "I don't want them on my db for some reason" Then rely on sever side code - client side Javascript cannot be trusted to function all the time. Commented Sep 20, 2010 at 15:15
  • yeah of course. But i need a control on client side too :) Commented Sep 20, 2010 at 15:18
  • I recommend reformulating your question with valid JavaScript syntax, so people are actually able to help you do what you're trying to do. It appears that the thing we all think you're trying to do is not what you're trying to do. The more effort you put into the question, the better the answers will be. Commented Sep 20, 2010 at 15:40
  • 2
    In the above, you have value.replace(/\\/g, '');. It needs to be value = value.replace(/\\/g, '');. replace doesn't change the string it's called on, it returns an updated string. See the examples in the answers below. Commented Sep 20, 2010 at 15:51
  • 1
    @markzzz — It sounds like you want to change the regex from /\\/g to /[\\\s]+/g, which will remove both forward slashes and whitespace. Commented Sep 20, 2010 at 16:00

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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...
You need the g modifier on the regex... It can't be just a string. /\\/g not '\\'
@markzzz: Look at thenduks' answer again. There's a big difference between /\\/ and /\\/g.
@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.
oh no sorry! of course should be my english fault. I really appreciate your helps dudes hehe! sorry, i don't want offend you!
|
2

As Yi Jiang mentioned, the only reliable way to do this is with server-side code. Now, of course, you could be using JavaScript server-side (I frequently do).

The answer is:

updatedString = originalString.replace(/\\/g, '');

...which is a RegExp that will globally replace all of the backslashes with nothing.

Some implementations require that you do this instead:

var re = /\\/g;
re.lastIndex = 0;
updatedString = originalString.replace(re, '');

...or the replacement may not work correctly the second time. (Yes, really, and yes I know it seems wrong.) Actually, no, sorry — you only need the reset if you're not looping all the way to the end of the string (even if it seems like the RegEx should go out of scope, which is the strange bit). Since replace goes to the end of the string, you're fine.

Comments

0

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.

4 Comments

No, myvar=myvar.replace('\\',''); will only replace the first backslash.
i also tried your code. but if i put value as "\\" or "\" or "\\\\" the script say YES and i don't want this :)
@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.
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 :)

Your Answer

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.