- Up until now, you've looked at regexes to do literal matches of strings. But sometimes, you might want to also match case differences. Case (or sometimes letter case) is the difference between uppercase letters and lowercase letters.
You can match both cases using what is called a flag. There are other flags but here you'll focus on the flag that ignores case - the
iflag. You can use it by appending it to the regex. An example of using this flag is
/Randy/i. This regex can match the strings
let myString = "TItaN"; let myRegex = /Titan/i; let result = myRegex.test(myString); console.log(result); will display true
- So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the
- To use the
.match()method, apply the method on a string and pass in the regex inside the parentheses.
- Note: that the
.matchsyntax is the "opposite" of the
.testmethod you have been using thus far.
let extractStr = "Extract the word 'coding' from this string."; let codingRegex = /coding/; let result = extractStr.match(codingRegex);
console.log(result); will display ["coding"]
- So far, you have only been able to extract or search a pattern once.
- To search or extract a pattern more than once, you can use the
let twinkleStar = "Twinkle, twinkle, little star"; let starRegex = /twinkle/gi; let result = twinkleStar.match(starRegex); console.log(result); will display [ "Twinkle", "twinkle" ]
- Here we used the regex
starRegex, found and extracted both
Twinklewords from the string
- Note: You can have multiple flags on your regex like
- Sometimes you won't (or don't need to) know the exact characters in your patterns. It would take a long time to do so. Luckily, you can save time using the wildcard character:
- The wildcard character
.will match any one character. The wildcard is also called
period. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match run, sun, fun, pun, nun, bun, you can use the regex /.un/ to match all six words.
let exampleStr = "Let's have fun with regular expressions!"; let unRegex = /.un/; let result = unRegex.test(exampleStr); console.log(result); will display true here
- You learned how to match literal patterns (
/literal/) and wildcard character (
/./). Those are the extremes of regular expressions, where one finds exact matches and the other matches everything. There are options that are a balance between the two extremes.
- Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets.
- For example, you want to match character classes with vowels (a, e, i, o, u) in your regex vowelRegex to find all the vowels in the string quoteSample.
- Note: Be sure to match both upper- and lowercase vowels.
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it."; let vowelRegex = /[aeiou]/gi; let result = quoteSample.match(vowelRegex);
console.log(result); will display [ 'e', 'a', 'e', 'o', 'u', 'i', 'e', 'a', 'o', 'e', 'o', 'e', 'I', 'a', 'e', 'o', 'o', 'e', 'i', 'o', 'e', 'o', 'i', 'e', 'i' ]
- You saw how you can use character sets to specify a group of characters to match, but that's a lot of typing when you need to match a large range of characters (for example, every letter in the alphabet). Fortunately, there is a built-in feature that makes this short and simple.
- Inside a character set, you can define a range of characters to match using a hyphen character:
- For example, Let's match all the letters in the string quoteSample.
- Note: Be sure to match both uppercase and lowercase letters.
let quoteSample = "The quick brown fox jumps over the lazy dog."; let alphabetRegex = /[a-z]/gi; let result = quoteSample.match(alphabetRegex); console.log(result); will display [ 'T', 'h', 'e', 'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n', 'f', 'o', 'x', 'j', 'u', 'm', 'p', 's', 'o', 'v', 'e', 'r', 't', 'h', 'e', 'l', 'a', 'z', 'y', 'd', 'o', 'g' ]
- Using the hyphen (
-) to match a range of characters is not limited to letters. It also works to match a range of numbers.
- For example,
/[0-5]/matches any number between
5, including the
- Also, it is possible to combine a range of letters and numbers in a single character set.
let quoteSample = "Blueberry 3.141592653s are delicious."; let myRegex = /[h-s2-6]/ig; let result = quoteSample.match(myRegex);
console.log(result); will display [ 'l', 'r', 'r', '3', '4', '5', '2', '6', '5', '3', 's', 'r', 'l', 'i', 'i', 'o', 's' ]
- Here we created a single regex that matches a range of letters between h and s, and a range of numbers between 2 and 6. We didn't forget to include the appropriate flags in the regex.
- So far, we have created a set of characters that we wanted to match, but we could also create a set of characters that we do not want to match. These types of character sets are called negated character sets.
- To create a negated character set, you place a caret character (
^) after the opening bracket and before the characters you do not want to match.
- For example,
/[^aeiou]/gimatches all characters that are not a vowel. Note that characters like
,/` and white space are matched - the negated vowel character set only excludes the vowel characters.
- Let's create a single regex that matches all characters that are not a number or a vowel. Remember to include the appropriate flags in the regex.
let quoteSample = "3 blind mice."; let myRegex = /[[^aeiou0-9]/gi; let result = quoteSample.match(myRegex);
console.log(result); will display
[ ' ', 'b', 'l', 'n', 'd', ' ', 'm', 'c', '.' ]
- Sometimes, you need to match a character (or group of characters) that appears one or more times in a row. This means it occurs at least once, and may be repeated.
- You can use the
+character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other.
- Ex: You want to find matches when the letter
soccurs one or more times in
Mississippi. Write a regex that uses the
` let difficultSpelling = "Mississippi"; let myRegex = /s+/gi; let result = difficultSpelling.match(myRegex); `
` console.log(result); will display [ "ss", "ss" ] `
- It found two matches and return [ "ss", "ss" ] because the a characters are not in a row.