Skip to content

Instantly share code, notes, and snippets.

@BobGu
Created February 20, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobGu/144030229bfc6184cb03 to your computer and use it in GitHub Desktop.
Save BobGu/144030229bfc6184cb03 to your computer and use it in GitHub Desktop.
def make_bold(text)
#find all the words or phrases that are surrounded by **
#flatten to return just an array instead of an array of arrays
matches = text.scan(/\*{2}(.*?)\*{2}/).flatten
#replace those words in our text with strong tags by finding the pattern again
#continue to do this block of code while there is a match for words or phrases surrounded by **
while text.match(/\*{2}(.*?)\*{2}/)
text.sub!(/\*{2}(.*?)\*{2}/, "<strong>#{matches[0]}</strong>")
#delete the first match from our matches array since we have already used it to replace our text
matches.delete_at(0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment