Escaping a forward slash in a regular expression

Zerobu picture Zerobu · May 20, 2011 · Viewed 186.7k times · Source

My question is a simple one, and it is about regular expression escaping. Do you have to escape a forward slash / in a regular expression? And how would you go about doing it?

Answer

Crayon Violent picture Crayon Violent · May 20, 2011

What context/language? Some languages use / as the pattern delimiter, so yes, you need to escape it, depending on which language/context. You escape it by putting a backward slash in front of it: \/ For some languages (like PHP) you can use other characters as the delimiter and therefore you don't need to escape it. But AFAIK in all languages, the only special significance the / has is it may be the designated pattern delimiter.