Pattern matching in regex in ruby or rails

I am new to regex and looked into the possible matching expression. But I can’t able to find the thing I needed. What I want is remove the word that matches certain expression. The expression is to match all the words ignoring whitespaces, tabs and newline. I am not able to find the correct regex in ruby.

For example:

string = 'hello world welcome'

The thing I need to do is to replace certain words matches with word starting with ‘w’ and end with ‘d’.

string.gsub(/^w.*d$/, 'human')

but I am not able to ignore(escape) spaces(not replacing), tabs and new lines in that.

Can someone help with this. I tried with this below regexp to escape but its not happening.

string.gsub(/^w.*d$s/, 'human')

'hello world welcome' must be changed to 'hello human welcome' without removing spaces, tabs and newlines in the string.

Is that anywhere I can know more about regexp especially with ruby.


If I properly understand your question the regular expression would be:


See the pattern in action on Regexr, which helps generate regular expressions and tells you exactly what you are doing.

Cary Swoveland’s response is also good.