Skip to content

Instantly share code, notes, and snippets.

@Chryus
Created January 25, 2014 14:10
Show Gist options
  • Save Chryus/8616950 to your computer and use it in GitHub Desktop.
Save Chryus/8616950 to your computer and use it in GitHub Desktop.
The + character will make your regex greedy. The ? character will make your regex lazy.
//the plus character is greedy. It matches the preceding character as many times as possible.
str = "maroooooooooned on a desert island"
reg = /(o+)/
res = str.match(reg); //=> 'ooooooooo'
//we can make the plus operator lazy by adding a question mark after it.
//note that the only thing changed is the insertion of the special character "?", which tells the RegEx plus operator to calm down and not be such a glutton for o's.
str = "maroooooooooned on a desert island"
reg = /(o+?)/
res = str.match(reg); //=> 'o'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment