Module: JekyllMiscellaneous::Filters::RegexReplace
- Defined in:
- lib/jekyll_miscellaneous/filters/regex_replace.rb
Instance Method Summary collapse
-
#clean_urls(input) ⇒ Object
Replaces all occurrences of URLs in a string with a nothing.
-
#regex_replace(input, regex, replacement) ⇒ Object
Replaces all occurrences of a regular expression in a string with a replacement.
Instance Method Details
#clean_urls(input) ⇒ Object
Replaces all occurrences of URLs in a string with a nothing.
Note:
It will strip and squeeze the result string.
Parameters:
- input
-
The string to clean the urls from.
Returns:
The string without URLs.
Example:
regex_replace_urls('Hello, http://example.com World!')
# => "Hello, World!"
43 44 45 |
# File 'lib/jekyll_miscellaneous/filters/regex_replace.rb', line 43 def clean_urls(input) regex_replace(input.delete('…'), '(?<=^|[\s,])([\w-]+\.[a-z]{2,}\S*)\b', '').strip.squeeze(' ') end |
#regex_replace(input, regex, replacement) ⇒ Object
Replaces all occurrences of a regular expression in a string with a replacement.
Parameters:
- input
-
The string to replace the regular expression in.
- regex
-
The regular expression to replace, as a string.
- replacement
-
The string to replace the regular expression with.
Returns:
The string with all occurrences of the regular expression replaced.
Example:
regex_replace('Hello, World!', '$Hello', 'Goodbye')
# => "Goodbye, World!"
23 24 25 26 |
# File 'lib/jekyll_miscellaneous/filters/regex_replace.rb', line 23 def regex_replace(input, regex, replacement) formatted_regex = /#{regex}/ input.gsub(formatted_regex, replacement) end |