user2721609 February 2016

Not allowing \ through regular expression

I have created the below regular expression:

"format":"^[^/]*$"

To not allow forward slash and it works. I also doesn't have to allow forward slash. How can I modify this regular expression.

I tried the below one but it doesn't work:

"format":"^[^/\\]*$"

Answers


RAnders00 February 2016

The backslash is a special character in regular expressions. That's why you need to escape it with another backslash, so it loses its special meaning (don't copy it into your code yet, look below):

"format":"^[^/\\]*$"

If your programming language also uses backslashes for escaping, you might need to apply 4 backslashes: First, they are parsed by the compiler, and later, the regex engine will process them another time.

"format":"^[^/\\\\]*$"

Post Status

Asked in February 2016
Viewed 1,513 times
Voted 5
Answered 1 times

Search




Leave an answer