“Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects.” — MDN web docs
There’s a saying that goes, ‘face what you fear.’ Every time I looked at regex I cringed because I had no idea what all those forward slashes, back slashes and letters and numbers meant but I decided to face my fear and dive into understanding it.
Flags:
/i : ignores case of letters
Example:const myString = 'Face what you FEAR!'let ignoreCase = /fear/ilet result = myString.match(ignoreCase)returns true
/g : finds more than the first match
Example:const myString = 'Face what you fear! Your fear does not define you.'let globalRegex = /fear/glet result = myString.match(globalRegex)returns true
^ Place the ^symbol in front of the characters you do not want to match.
Example:const myString = 'Face what you fear!'let regex = /[^f]/gi let result = myString.match(regex)returns ["a", "c", "e", " ", "w", "h", "a", "t", " ", "y", "o", "u", " ", "e", "a", "r", "!"]
^ symbol also allows you to search for characters in the beginning of strings. You will remove the square brackets for this.
Example:const myString = 'Face what you fear!'let regex = /^face/ilet result = regex.test(myString)returns true
+ symbol matches characters or groups of characters that appear more than one time in a row.
Example:const word = 'Succulent!'let regex = /[c+]/glet result = word.match(regex)returns cc
$ symbol searches for characters at the end of the string
Example:const string = 'Succulents are drought tolerant'let regex = /tolerant$/let result = regex.test(string)returns true
* symbol matches characters that appear zero or more times
let word = 'wooooohoo!'
let animal = 'wombat'
let mistake = 'whoops'
let regex = /wo*/word.match(regex) // returns 'wooooo'
animal.match(regex) // returns 'wo'
mistake.match(regex) // returns 'w'
[a-z0–9] and /\w/ are the same which allows you to match letters a-z and numbers 0–9. /\w/ is the shortcut version. This was the most confusing for me to look at but now I have solved the mystery!
Example:
let longWay = /[A-Za-z0-9_]+/
let shortWay = /\w+/
let num = '100'
let thing = 'something'longWay.test(num) // returns true
shortWay.test(num) // returns true
longWay.test(thing) // returns true
shortWay.test(thing) // returns true
[^A-Za-z0–9_] is the same as \W. It allows you to find what is non-alphanumeric.
Example:
let shortWay = /\W/
let num = '100%'
let thing = 'something?'num.match(shortWay) // returns %
thing.match(shortWay) //returns ?
\d is a shortcut version to look for digits only
Example:
let string = 'We live in the year 2020.'
let regex = /\d/gstring.match(regex) // returns 2020
\D is a shortcut that looks for non-digit characters
Example:
let string = 'We live in the year 2020.'
let regex = /\D/gstring.match(regex) // returns 'We live in the year .'
\s looks for whitespaces or spaces
let string = 'How many whitespaces does this sentence have?'
let regex = /\s/gstring.match(regex).length // returns 6
\S returns for non-whitespaces
let string = 'How many whitespaces does this sentence have?'
let regex = /\S/gstring.match(regex).length // returns 39
There are a few more characters but these are the basics. Finally, these random numbers and letters make a little bit more sense. It will take a little bit of practice to get used to but it can prove to be helpful when solving algorithms that require regex.