# Why does Regexp::escape backslash spaces?!?

**URL:** <https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349>\
**Category:** Ruby\
**Created:** [January 26, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349 "2010-01-26T21:12:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 26, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349/1 "2010-01-26T21:12:09Z")

</div>

$ ruby -v  
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]  
$ irb  
irb(main):001:0\> Regexp::escape(‘a b’)  
=\> “a\ b”

Why? This doesn’t seem to make any sense. Is it a bug?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 26, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349/2 "2010-01-26T22:46:32Z")

</div>

Marnen Laibow-Koser wrote:

> irb(main):001:0\> Regexp::escape(‘a b’)  
> =\> “a\ b”
> 
> Why? This doesn’t seem to make any sense. Is it a bug?

Maybe spaces are escaped so the resulting string be compatible with an  
“extended” pattern, where non-escaped spaces are ignored:

/ (\w+) \s (\w+) /x

is the same as:

/(\w+)\s(\w+)/

---

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 26, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349/3 "2010-01-26T22:53:11Z")

</div>

Albert S. wrote:

> Marnen Laibow-Koser wrote:
> 
> > irb(main):001:0\> Regexp::escape(‘a b’)  
> > =\> “a\ b”
> > 
> > Why? This doesn’t seem to make any sense. Is it a bug?
> 
> Maybe spaces are escaped so the resulting string be compatible with an  
> “extended” pattern, where non-escaped spaces are ignored:
> 
> / (\w+) \s (\w+) /x
> 
> is the same as:
> 
> /(\w+)\s(\w+)/

Good point. And anyway, it causes no problems: when I first posted, I  
thought it did, but those problems actually came from elsewhere.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 26, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/why-does-regexp-escape-backslash-spaces/182349/4 "2010-01-26T23:16:54Z")

</div>

On Tue, Jan 26, 2010 at 1:12 PM, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> $ ruby -v  
> ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]  
> $ irb  
> irb(main):001:0\> Regexp::escape(‘a b’)  
> =\> “a\ b”
> 
> Why? This doesn’t seem to make any sense. Is it a bug?

It is intentional. In re.c:rb\_reg\_quote()

```
    switch (c) {
      case '[': case ']': case '{': case '}':
      case '(': case ')': case '|': case '-':
      case '*': case '.': case '\\':
      case '?': case '+': case '^': case '$':
      case ' ': case '#':
      case '\t': case '\f': case '\n': case '\r':
        goto meta_found;
    }

```

And then down in meta\_found:

```
      case ' ':
        *t++ = '\\';
        *t++ = ' ';
        continue;

```

Kirk H.
