I just found Regular Expression - replace word except within a URL/URI and modify the code to be like this:
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"
def remove_uris(text)
text.split(URI_REGEX).collect do |s|
unless s =~ URI_REGEX
s
end
end.join
end
I test it in rails console and it worked as expected:
remove_uris('bla bla bla... bla bla bla... http://bit.ly/someuri bla bla bla...')
=> "bla bla bla... bla bla bla... bla bla bla..."
If anyone have better / effective solution, I will vote up or accept it. Thanks.
'http://bit.ly/the url with space'will become'url with space'