Skip to content

Instantly share code, notes, and snippets.

@tyre
Last active August 29, 2015 14:10
Show Gist options
  • Save tyre/ed013c3c7b56b5a25a5c to your computer and use it in GitHub Desktop.
Save tyre/ed013c3c7b56b5a25a5c to your computer and use it in GitHub Desktop.
url = "https://google.com/:bubbles/frosted"
# We want "/:bubbles/" to match, but not "/:bubbles_waffle/" to match
regex = ~r/(\/|\a)\:bubbles(\/|\Z)/
# Sad Times!
# Expect ":bubbles" to be replaced with "33"
String.replace url, regex, "\\133\\2"
# => "https://google.com/frosted"
# Works!
# Expect ":bubbles" to be replaced with " 33 "
String.replace url, regex, "\\1 33 \\2"
# => "https://google.com/ 33 /frosted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment