Last active
August 3, 2020 19:28
-
-
Save aramkoukia/2701521655888d172ebc00f2223e0409 to your computer and use it in GitHub Desktop.
javascript string search
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
| // 1. includes (introduced in ES6) | |
| var string = "string to search for substring", | |
| substring = "sea"; | |
| string.includes(substring); | |
| // 2. RegExp: test | |
| var string = "string to search for substring", | |
| expr = /sea/; // no quotes here | |
| expr.test(string); | |
| //3. string.search | |
| var string = "string to search for substring", | |
| expr = "/sea/"; | |
| string.search(expr); | |
| //4. lodash: includes | |
| var string = "string to search for substring", | |
| substring = "sea"; | |
| _.includes(string, substring); | |
| // 5. string.match | |
| var string = "string to search for substring", | |
| expr = "/sea/"; | |
| string.match(expr); | |
| // 3. string.indexOf | |
| var string = "string to search for substring", | |
| substring = "sea"; | |
| string.indexOf(substring) !== -1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment