What are Regular Expressions?

/love/g"I love learning new things"
/e/g"I love learning new things in my spare time, even while sleeping"
/e+/g"I love learning new things in my spare time, even while sleeping"
/ea?/g"I love learning new things in my spare time, even while sleeping"
/\w/g 
//matches any word character
/\s/g
//matches any white space
/\w{4}/g 
//matches any 4 digits in a row
/\w{4,}/g
//matches 4 or more digits in a row
/\w{4,5}
//matches any set of 4 or 5 characters in a row
/[a-z]/g 
//matches any lower case character from a-z
/[a-z][A-Z]/g
//matches any lower or upper case character from a-z ...which is all of them
/[0-9]/g
//we can also validate numbers too!
/^I/g //matches the beginning of the text if it begins with an upper case 'I'
1234567890 //if this is how you write out your number, we can't be friends123-456-7890123 456 7890(123) 456-7890 //this is how I personally do it ;)
/\d{10}/g//this searches for 10 digits in a row, but will only work for the first example.
/\d{3}-?\d{3}-?\d{4}/g//this matches for groups of three with an optional dash, followed by a group of 4. This will match the first two examples./\d{3}[ -]?\d{3}[ -]?\d{4}/g//notice adding the space in the group with the dash now matches the first three examples.
/\(?\d{3}\)?[ -]?\d{3}[ -]?\d{4}/g//This will match all four examples!

--

--

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