157

I'm using the following regular expression

^[a-zA-Z0-9\',!;\?\$\^:\\\/`\|~&\" @#%\*\{}\(\)_\+\.\s=-]{1,1000}$

I know it's ugly, but so far it serves its purpose other than the backslash not being allowed as I think it should because it's escaped. Also, I tried \\ instead of \\\ but got the same results.
Any ideas?

Henke
  • 4,445
  • 3
  • 31
  • 44
Eton B.
  • 6,121
  • 5
  • 31
  • 43

7 Answers7

289

If you're putting this in a string within a program, you may actually need to use four backslashes (because the string parser will remove two of them when "de-escaping" it for the string, and then the regex needs two for an escaped regex backslash).

For instance:

regex("\\\\")

is interpreted as...

regex("\\" [escaped backslash] followed by "\\" [escaped backslash])

is interpreted as...

regex(\\)

is interpreted as a regex that matches a single backslash.


Depending on the language, you might be able to use a different form of quoting that doesn't parse escape sequences to avoid having to use as many - for instance, in Python:

re.compile(r'\\')

The r in front of the quotes makes it a raw string which doesn't parse backslash escapes.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 29
    hehe... I just ran into this and needed to add three. I just kept adding backslashes until it worked. – But those new buttons though.. Apr 27 '15 at 15:01
  • ummm, why on earth is this regex reinterpreted twice instead of once like it's supposed to be for PCRE? – Jim Michaels Sep 04 '16 at 02:26
  • 4
    @JimMichaels because not all languages have unescaped regex literals, and thus sometimes the programming language itself interprets slash escapes once in its string syntax, and the resulting string then gets passed to the regex engine (which interprets slash escapes in regex syntax). – Amber Dec 06 '16 at 21:32
  • 1
    once, long ago, I had to capture an xpath of an element whose only identifying feature was a windows file path, then generate a program (as a string) in which xpaths to be represented as strings. at one point in the intermediate steps there were 8 backslashes used to represent a single backslash in the file path. That's the highest I've ever gotten though. – Zackkenyon Jul 06 '17 at 17:10
  • This can become even more convoluted when searching for backslashes along with patterns that require meta characters. Take for example, finding a backslash followed by a digit. Now you'd be staring at the following expression trying to figure out what's going on: `new RegExp('\\\\\\d');`. – jabacchetta May 27 '18 at 18:04
  • Thank you! This solved a gnarly negation search issues in Swift for me. – brian_schick Jun 04 '19 at 19:02
24

If it's not a literal, you have to use \\\\ so that you get \\ which means an escaped backslash.

That's because there are two representations. In the string representation of your regex, you have "\\\\", Which is what gets sent to the parser. The parser will see \\ which it interprets as a valid escaped-backslash (which matches a single backslash).

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
15

The backslash \ is the escape character for regular expressions. Therefore a double backslash would indeed mean a single, literal backslash.

\ (backslash) followed by any of [\^$.|?*+(){} escapes the special character to suppress its special meaning.

ref : http://www.regular-expressions.info/reference.html

Brad
  • 15,361
  • 6
  • 36
  • 57
5

From http://www.regular-expressions.info/charclass.html :

Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

To include a backslash as a character without any special meaning inside a character class, you have to escape it with another backslash. [\\x] matches a backslash or an x. The closing bracket (]), the caret (^) and the hyphen (-) can be included by escaping them with a backslash, or by placing them in a position where they do not take on their special meaning. I recommend the latter method, since it improves readability. To include a caret, place it anywhere except right after the opening bracket. [x^] matches an x or a caret. You can put the closing bracket right after the opening bracket, or the negating caret. []x] matches a closing bracket or an x. [^]x] matches any character that is not a closing bracket or an x. The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. Both [-x] and [x-] match an x or a hyphen.

What language are you writing the regex in?

Community
  • 1
  • 1
Nate
  • 12,499
  • 5
  • 45
  • 60
1

you can use ? at the end to skip one "\"

regex("\\\\?")
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

This solution fixed my problem while replacing br tag to '\n' .

alert(content.replace(/<br\/\>/g,'\n'));
hltsydmr
  • 59
  • 3
-1

in some cases, it's possible to use . instead of \ and avoid escaping the backslash

enter image description here

player0
  • 124,011
  • 12
  • 67
  • 124