# Regex question

**URL:** <https://www.ruby-forum.com/t/regex-question/141454>\
**Category:** Rails\
**Created:** [June 26, 2008, 12:14am UTC](https://www.ruby-forum.com/t/regex-question/141454 "2008-06-26T00:14:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 26, 2008, 12:14am UTC](https://www.ruby-forum.com/t/regex-question/141454/1 "2008-06-26T00:14:06Z")

</div>

I’m trying to replace all characters that are not letters numbers and  
white spaces in a string. I’m getting the characters eliminated in this  
case a comma but the white space is eliminated to. How do I get around  
that?

string = ‘john, tony’  
newTerms = string.gsub(/\W/, “”)

thanks

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 26, 2008, 12:49am UTC](https://www.ruby-forum.com/t/regex-question/141454/2 "2008-06-26T00:49:55Z")

</div>

On Jun 25, 2008, at 3:14 PM, Sam G. wrote:

> I’m trying to replace all characters that are not letters numbers and  
> white spaces in a string. I’m getting the characters eliminated in  
> this  
> case a comma but the white space is eliminated to. How do I get around  
> that?
> 
> string = ‘john, tony’  
> newTerms = string.gsub(/\W/, “”)

> newTerms = string.gsub(/[^\w\s]/, “”)  
> =\> “john tony”

is one way…

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 26, 2008, 1:03am UTC](https://www.ruby-forum.com/t/regex-question/141454/3 "2008-06-26T01:03:35Z")

</div>

Philip H. wrote:

> > string = ‘john, tony’  
> > newTerms = string.gsub(/\W/, “”)
> 
> > newTerms = string.gsub(/[^\w\s]/, “”)  
> > =\> “john tony”
> 
> is one way…

thanks a million
