Created
January 25, 2014 14:10
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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