RegEx Flashcards
what the test() method does
takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not.
let testStr = “freeCodeCamp”;
let testRegex = /Code/;
testRegex.test(testStr);
// Returns true
search for multiple patterns using the
alternation or OR operator: |
if you wanted to match “yes” or “no”, the regex you want is /yes|no/.
match both cases using
a flag
You can use it by appending it to the regex. An example of using this flag is /ignorecase/i. This regex can match the strings “ignorecase”, “igNoreCase”, and “IgnoreCase”
extract actual matches with ___
match() method
To search or extract a pattern more than once…
use the g flag
what the wildcard character . does
will match any one character
if you wanted to match “hug”, “huh”, “hut”, and “hum”, you can use the regex /hu./ to match all four words
what are character classes
Character classes allow you to define a group of characters you wish to match by placing them inside square [ ] brackets.
match “bag”, “big”, and “bug” but not “bog”
/b[aiu]g/
define a range of characters to match
[a-e] finds characters a to e
/[0-5]/ matches
any number between 0 and 5 including 0 and 5
matches all letters and numbers in word
/[a-z0-9]/ig
create a negated character set with
you place a caret character (^) after the opening bracket and before the characters you do not want to match.
/[^aeiou]/gi matches all characters that are not a vowel
how to match a character (or group of characters) that appears one or more times in a row
use the + character
find matches when the letter s occurs one or more times in “Mississippi”
/s+/g
option that matches characters that occur zero or more times
asterisk or star: *
greedy vs lazy match
a greedy match finds the longest possible part of a string
a lazy match finds the smallest possible part of the string
Regular expressions are by default {greedy or lazy?)
greedy
you can use the ____ character to change it to lazy matching
?
“titanic” matched against the adjusted regex of /t[a-z]*?i/ returns
[“ti”]
“titanic” matched against the adjusted regex of /t[a-z]*i/ returns
[“titani”]