loading...
Cover image for Regular Expressions in 1 Minute

Regular Expressions in 1 Minute

connoro7 profile image Connor Dillon ・3 min read

Syntax

Sets the regex query for "hello":

re = /hello/

Sets the regex query to be case-insensitive:

re = /hello/i

Sets the regex query as a global search, searching for all instances, not just the first:

re = /hello/g

Special Characters

Literal Characters

Matches with any string containing exactly hello, and is case-sensitive:

re = /hello/

Matches with any string containing hello, and is case-insensitive:

re = /hello/i

Meta-character Symbols

^ "Must start with"

Matches with "Hello World":

re = /^h/i

Matches with "Hello World":

re = /^hel/i

$ "Must end with"

Matches with "Hello World":

re = /d$/i

Matches with "Hello World":

re = / world$/i

^...$ "Must begin with and end with"

Not a match with "Hello World", only Matches "Hello":

re = /^hello$/i

. "Matches any ONE character"

Basically a wild card but for ONLY ONE character, try changing str to "Hbllo World"

Matches with "Hello" or "Hbllo" or "Hwllo" or "H7llo" or "H@llo", etc.:

re = /h.llo/i

* "Matches any character 0 or more times"

This is a true wild card (also works for zero characters!)"

Matches with "hello" or "heeeello" or "heebbllo" or "h52340978562llo" or "H!@#\$&^!%#*@%!%llo", etc.:

re = /h\*llo/i

? "Optional character"

Matches with "hello" or "hallo" or "hullo" or "hllo":

re = /ha?e?u?llo/i

\ "Escape character"

Not a match with "Hello", only Matches "Hello?":

re = /hello\?/i

[ ] Brackets Character sets

Matches "hello" or "hallo", but not "hllo" or anything else:

re = /h[ae]llo/i

Matches "Hello" or "Zello":

re = /[HZ]ello/i

Not a match; [^HZ] = anything EXCEPT H or Z":

re = /[^HZ]ello/i

Matches ANY uppercase letter:

re = /[A-Z]ello/

Matches ANY lowercase letter:

re = /[a-z]ello/

Matches ANY letter with any case:

re = /[A-Za-z]ello/

Only matches a number like "1ello" or "9ello", NOTE: "1234ello" also matches because it's just looking for a single digit before "ello":

re = /[0-9]ello/

Only matches a double digit number like "69ello" or "420ello", NOTE: still only looks for 2 digits before "ello":

re = /[0-9][0-9]ello/

{ } Braces Quantifiers

Matches if l occurs exactly {m} amount of times:

re = /Hel{2}o/

Matches if l occurs 2-4 times:

re = /Hel{2,4}o/

Matches if l occurs at least 2 times:

re = /Hel{2,}o/

( ) Parentheses Grouping

Matches as long as it finds Xp repeating 3 times:

re = /([0-9]p){3}/

Matches only if it finds Xp repeating exactly 3 times:

re = /^([0-9]){3}$/

Shorthand Character Classes

\w "Word Character"

Matches for any letter, number, or underscore _, but no other symbols or characters:

re = /\w/

+ "One or More"

Matches for one or more letter, number, or underscore _:

re = /\w+/

\W "Non-Word Character"

Matches only if it finds something that's NOT a letter, number, or underscore:

re = /\W/

\d "Digit Character"

Matches for a single digit:

re = /\d/

Matches for one or more digits:

re = /\d+/

\D "Non-Digit Character"

Matches for any non-digit character:

re = /\D/

\s "Whitespace Character"

Matches for a space, tab, or similar whitespace characters:

re = /\s/

\S "Non-Whitespace Character"

Matches for anything other than a space, tab, or similar whitespace characters:

re = /\S/

\b "Word Boundary"

Matches for words that have a word boundary at that position, so that you avoid matching strings that contain a word inside of another word, like with Hell & Hello or Beetle & Beetlejuice:

re = /Hell\b/i

Assertions

x(?=y) "x followed by y"

Matches x only if followed by y:

re = /x(?=y)/i

x(?!y) "x not followed by y"

Matches x only if NOT followed by y:

re = /x(?!y)/i

Reference: Strings we've used to match in this article

const str = 'Hello World'
const str = '3p3p3p'
const str = 'Hello, welcome to Hell'
const str = 'asgkljhalwxqflife'

Posted on by:

connoro7 profile

Connor Dillon

@connoro7

Flannel and coffee enthusiast living the cliché software developer life in Seattle, WA. Huge fan of other people's dogs, music production, and fitness.

Discussion

markdown guide