Skip to main content

RegExp

Definition

A RegExp (regular expression) is a pattern used to match character combinations in strings. Regular expressions are powerful tools for pattern matching and text manipulation.

typeof /pattern/  // "object"
/pattern/ instanceof RegExp // true

Creating regular expressions

Literal Syntax

const regex = /pattern/
const regexWithFlags = /pattern/gi

Constructor Syntax

const regex = new RegExp('pattern')
const regexWithFlags = new RegExp('pattern', 'gi')

Dynamic Patterns

Use constructor when pattern is dynamic:

const pattern = 'hello'
const regex = new RegExp(pattern, 'i')

Flags

Regular expressions can have flags that modify behavior:

  • g - Global (find all matches, not just first)
  • i - Case-insensitive
  • m - Multiline
  • s - Dotall (. matches newlines)
  • u - Unicode
  • y - Sticky
/hello/gi  // Global and case-insensitive

Common patterns

Literal Characters

/hello/  // Matches "hello"

Character Classes

/[abc]/        // Matches a, b, or c
/[a-z]/ // Matches any lowercase letter
/[0-9]/ // Matches any digit
/[^abc]/ // Matches anything except a, b, or c
/\d/ // Matches any digit (same as [0-9])
/\w/ // Matches word character (letter, digit, underscore)
/\s/ // Matches whitespace

Quantifiers

/a*/           // Zero or more a's
/a+/ // One or more a's
/a?/ // Zero or one a
/a{3}/ // Exactly 3 a's
/a{3,}/ // 3 or more a's
/a{3,5}/ // 3 to 5 a's

Anchors

/^hello/       // Starts with "hello"
/hello$/ // Ends with "hello"
/^hello$/ // Exactly "hello"
/\bword\b/ // Word boundary

Groups

/(abc)/        // Capturing group
/(?:abc)/ // Non-capturing group
/(a|b)/ // Alternation (a or b)

Regexp methods

test()

Tests whether a pattern matches a string:

/hello/.test("hello world")  // true
/hello/.test("hi there") // false

exec()

Executes a search and returns match information:

const regex = /hello/
const result = regex.exec("hello world")
// result is ["hello", index: 0, input: "hello world", groups: undefined]

String methods with regexp

match()

Returns matches:

"hello world".match(/hello/)  // ["hello"]
"hello hello".match(/hello/g) // ["hello", "hello"]

Returns index of first match:

"hello world".search(/world/)  // 6
"hello world".search(/xyz/) // -1

replace()

Replaces matches:

"hello world".replace(/world/, "JavaScript")  // "hello JavaScript"
"hello hello".replace(/hello/g, "hi") // "hi hi"

split()

Splits string by pattern:

"a,b,c".split(/,/)      // ["a", "b", "c"]
"hello world".split(/\s/) // ["hello", "world"]

Common use cases

Email Validation

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
emailRegex.test("user@example.com") // true

Phone Number

const phoneRegex = /^\+?[\d\s-()]+$/
phoneRegex.test("+1-555-123-4567") // true

Extract Numbers

const numbers = "Price: $123.45".match(/\d+\.?\d*/)
// ["123.45"]

Remove Whitespace

"  hello  world  ".replace(/\s+/g, " ")  // " hello world "

Best practices

  1. Use literal syntax when possible: More readable and slightly faster.

  2. Be careful with special characters: Escape special regex characters with \.

  3. Test your patterns: Regular expressions can be tricky—test thoroughly.

  4. Consider performance: Complex regex can be slow on large strings.

  5. Use flags appropriately: g flag for multiple matches, i for case-insensitive.