# Escaping for regexp?

**URL:** <https://www.ruby-forum.com/t/escaping-for-regexp/68957>\
**Category:** Ruby\
**Created:** [September 17, 2006, 9:43am UTC](https://www.ruby-forum.com/t/escaping-for-regexp/68957 "2006-09-17T09:43:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2006, 9:43am UTC](https://www.ruby-forum.com/t/escaping-for-regexp/68957/1 "2006-09-17T09:43:33Z")

</div>

i’ve a regexp like that :

/.\*#{name}$/ === path

however the variable “name” could, for example, in the form of :

“L2 Cache (per CPU)”

then, the string “name” might contain reserved chars for regexp,

i plan to escape them replacing “(” by “\(” etc…

however i wonder if such a function for escaping reserved chars does  
exists buit-in ???

notice i don’t want to escape “.\*” because i plan to use the wild card.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2006, 9:51am UTC](https://www.ruby-forum.com/t/escaping-for-regexp/68957/2 "2006-09-17T09:51:34Z")

</div>

Une bÃ©vue wrote:

> i plan to escape them replacing “(” by “\(” etc…
> 
> however i wonder if such a function for escaping reserved chars does  
> exists buit-in ???

Regexp.escape(name)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2006, 11:41am UTC](https://www.ruby-forum.com/t/escaping-for-regexp/68957/3 "2006-09-17T11:41:37Z")

</div>

Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Regexp.escape(name)

fine thanxs !!!
