Regex aka Regular Expressions

Example:const myString = 'Face what you FEAR!'let ignoreCase = /fear/ilet result = myString.match(ignoreCase)returns true
Example:const myString = 'Face what you fear! Your fear does not define you.'let globalRegex = /fear/glet result = myString.match(globalRegex)returns true
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", "!"]
Example:const myString = 'Face what you fear!'let regex = /^face/ilet result = regex.test(myString)returns true
Example:const word = 'Succulent!'let regex = /[c+]/glet result = word.match(regex)returns cc
Example:const string = 'Succulents are drought tolerant'let regex = /tolerant$/let result = regex.test(string)returns true
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'
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
Example: 
let shortWay = /\W/
let num = '100%'
let thing = 'something?'
num.match(shortWay) // returns %
thing.match(shortWay) //returns ?
Example: 
let string = 'We live in the year 2020.'
let regex = /\d/g
string.match(regex) // returns 2020
Example: 
let string = 'We live in the year 2020.'
let regex = /\D/g
string.match(regex) // returns 'We live in the year .'
let string = 'How many whitespaces does this sentence have?'
let regex = /\s/g
string.match(regex).length // returns 6
let string = 'How many whitespaces does this sentence have?'
let regex = /\S/g
string.match(regex).length // returns 39

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store