Ruby replace special characters with space

When I am seeking for this for Rails paperclip gem’s filename replacement, I found a very useful regular expression,

ruby-1.9.2-p290 :042 > name = "Abhi@## lash@###)(*&(!@"

=> "Abhi@## lash@###)(*&(!@"

ruby-1.9.2-p290 :043 > name.gsub(/([_@#!%()\-=;><,{}\~\[\]\.\/\?\"\*\^\$\+\-]+)/, ' ')

=> "Abhi lash & "

 
Unknown's avatar

Author: Abhilash

Hi, I’m Abhilash! A seasoned web developer with 15 years of experience specializing in Ruby and Ruby on Rails. Since 2010, I’ve built scalable, robust web applications and worked with frameworks like Angular, Sinatra, Laravel, Node.js, Vue and React. Passionate about clean, maintainable code and continuous learning, I share insights, tutorials, and experiences here. Let’s explore the ever-evolving world of web development together!

Leave a comment