DEV Community

Cover image for Regular Expressions in 1 Minute
Connor Dillon
Connor Dillon

Posted on

3 1

Regular Expressions in 1 Minute

Syntax

Sets the regex query for "hello":

re = /hello/
Enter fullscreen mode Exit fullscreen mode

Sets the regex query to be case-insensitive:

re = /hello/i
Enter fullscreen mode Exit fullscreen mode

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

re = /hello/g
Enter fullscreen mode Exit fullscreen mode

Special Characters

Literal Characters

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

re = /hello/
Enter fullscreen mode Exit fullscreen mode

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

re = /hello/i
Enter fullscreen mode Exit fullscreen mode

Meta-character Symbols

^ "Must start with"

Matches with "Hello World":

re = /^h/i
Enter fullscreen mode Exit fullscreen mode

Matches with "Hello World":

re = /^hel/i
Enter fullscreen mode Exit fullscreen mode

$ "Must end with"

Matches with "Hello World":

re = /d$/i
Enter fullscreen mode Exit fullscreen mode

Matches with "Hello World":

re = / world$/i
Enter fullscreen mode Exit fullscreen mode

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

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

re = /^hello$/i
Enter fullscreen mode Exit fullscreen mode

. "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
Enter fullscreen mode Exit fullscreen mode

* "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
Enter fullscreen mode Exit fullscreen mode

? "Optional character"

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

re = /ha?e?u?llo/i
Enter fullscreen mode Exit fullscreen mode

\ "Escape character"

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

re = /hello\?/i
Enter fullscreen mode Exit fullscreen mode

[ ] Brackets Character sets

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

re = /h[ae]llo/i
Enter fullscreen mode Exit fullscreen mode

Matches "Hello" or "Zello":

re = /[HZ]ello/i
Enter fullscreen mode Exit fullscreen mode

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

re = /[^HZ]ello/i
Enter fullscreen mode Exit fullscreen mode

Matches ANY uppercase letter:

re = /[A-Z]ello/
Enter fullscreen mode Exit fullscreen mode

Matches ANY lowercase letter:

re = /[a-z]ello/
Enter fullscreen mode Exit fullscreen mode

Matches ANY letter with any case:

re = /[A-Za-z]ello/
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

{ } Braces Quantifiers

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

re = /Hel{2}o/
Enter fullscreen mode Exit fullscreen mode

Matches if l occurs 2-4 times:

re = /Hel{2,4}o/
Enter fullscreen mode Exit fullscreen mode

Matches if l occurs at least 2 times:

re = /Hel{2,}o/
Enter fullscreen mode Exit fullscreen mode

( ) Parentheses Grouping

Matches as long as it finds Xp repeating 3 times:

re = /([0-9]p){3}/
Enter fullscreen mode Exit fullscreen mode

Matches only if it finds Xp repeating exactly 3 times:

re = /^([0-9]){3}$/
Enter fullscreen mode Exit fullscreen mode

Shorthand Character Classes

\w "Word Character"

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

re = /\w/
Enter fullscreen mode Exit fullscreen mode

+ "One or More"

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

re = /\w+/
Enter fullscreen mode Exit fullscreen mode

\W "Non-Word Character"

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

re = /\W/
Enter fullscreen mode Exit fullscreen mode

\d "Digit Character"

Matches for a single digit:

re = /\d/
Enter fullscreen mode Exit fullscreen mode

Matches for one or more digits:

re = /\d+/
Enter fullscreen mode Exit fullscreen mode

\D "Non-Digit Character"

Matches for any non-digit character:

re = /\D/
Enter fullscreen mode Exit fullscreen mode

\s "Whitespace Character"

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

re = /\s/
Enter fullscreen mode Exit fullscreen mode

\S "Non-Whitespace Character"

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

re = /\S/
Enter fullscreen mode Exit fullscreen mode

\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
Enter fullscreen mode Exit fullscreen mode

Assertions

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

Matches x only if followed by y:

re = /x(?=y)/i
Enter fullscreen mode Exit fullscreen mode

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

Matches x only if NOT followed by y:

re = /x(?!y)/i
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more