Skip to content

Instantly share code, notes, and snippets.

@iNecas
Created February 13, 2011 20:54
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 iNecas/825111 to your computer and use it in GitHub Desktop.
Save iNecas/825111 to your computer and use it in GitHub Desktop.
Advanced string splitting with regexp
string = "<div>ruby</div><div>string</div><div>methods</div>"
# == splitting with regexp
string.split(/<.*?>/)
# => ["", "ruby", "", "string", "", "methods"]
# == and keeping separator in the result
string.split(/(<.*?>)/)
# => ["", "<div>", "ruby", "</div>", "", "<div>", "string", "</div>", "", "<div>", "methods", "</div>"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment