I thought this question could be interesting to put it up here.
For example I have a string for search query like: "hello world"
There could be strings like this:
Hello World
Hello-world
hello!! world
How could you write an expression that will dynamically match these? If the expression works, it should works on this too:
Search: "Hi pals!"
Hi pals
Hi! Pals!
Hi-pals
Is it possible?
Sure, you could do something like this:
the trailing
iat the end enables case-insensitive matching, and the.*?matches any characters (except line breaks) between the"hi"and"pals".A Rubular demo: http://rubular.com/r/cwtbV2iTwy
Beware that it also matches a string like “Himalaya pals”:
If you don’t want that to happen, add some word-boundaries in the mix: