Skip to main content

Command Palette

Search for a command to run...

Regular Expressions (Regex) in JavaScript

Updated
3 min read
Regular Expressions (Regex) in JavaScript
S

I'm Shubham (@shubhamsinghbundela), I'm a Software Engineer, a Full-stack developer, a tech enthusiast, and a technical writer here on @Hashnode. I have a strong zeal to share my acquired knowledge and I am also willing to learn from others.

Regular Expressions (Regex) help us search, match, and manipulate text patterns inside strings.

1. Basic Format of Regex

/pattern/flags

Example:

/hello/g
  • / → Start and end of regex

  • hello → pattern

  • g → flag


2. Flags

Flag

Meaning

g

Global → match all occurrences

i

Case insensitive

m

Multi-line

g → Global (match all occurrences)

const str = "cat cat dog";
console.log(str.match(/cat/g));

Output:

["cat", "cat"]

Without g, it would return only the first match.

i → Case insensitive

const str = "Cat cat CAT";
console.log(str.match(/cat/gi));

Output:

["Cat", "cat", "CAT"]

3. + → One or More

/e+/g → Match at least one e

const str = "heeeello";
console.log(str.match(/e+/g));

Output:

["eeee"]

Explanation:

  • + means one or more times.

4. ? → Optional (0 or 1 time)

/e+a?/g

const str = "eeea eee";
console.log(str.match(/e+a?/g));

Output:

["eeea", "eee"]

Explanation:

  • e+ → one or more e

  • a? → a is optional


5. * → Zero or More

/ea*/

const str = "e ea eaaa";
console.log(str.match(/ea*/g));

Output:

["e", "ea", "eaaa"]

Explanation:

  • a* → zero or more a

6. . → Any Single Character

/.at/

const str = "cat fat hat at";
console.log(str.match(/.at/g));

Output:

["cat", "fat", "hat"]

Explanation:

  • . matches any one character before at.

7. Escape Character \

/.\./

const str = "a.b c.d";
console.log(str.match(/.\./g));

Output:

["a.", "c."]

Explanation:

  • \. means match actual dot (.).

8. \w → Word Character

Matches:

  • a-z

  • A-Z

  • 0-9

  • underscore _

const str = "Hello_123!";
console.log(str.match(/\w/g));

Output:

["H","e","l","l","o","_","1","2","3"]

9. \s → Whitespace

const str = "Hello World";
console.log(str.match(/\s/g));

Output:

[" "]

10. {min,max} → Quantifiers

/\w{1,3}/g

const str = "JavaScript";
console.log(str.match(/\w{1,3}/g));

Output:

["Jav", "aSc", "rip", "t"]

Explanation:

  • Match minimum 1 and maximum 3 characters.

11. Character Sets []

/[fc]at/

const str = "fat cat bat";
console.log(str.match(/[fc]at/g));

Output:

["fat", "cat"]

Explanation:

  • Match either f or c before at.

12. [a-z] → Character Range

Explanation: Match any character between a and z.

const str = "ABCdef";
console.log(str.match(/[a-z]/g));

Output:

["d", "e", "f"]

13. (a|b) → OR Operator

Explanation: Match either a or b.

const str = "Tiger tiger";
console.log(str.match(/(T|t)/g));

Output:

["T", "t"]

Better way:

console.log(str.match(/t/gi));

14. ^ → Start of Line

Explanation: Match only if string starts with pattern.

console.log(/^Hello/.test("Hello World"));

Output:

true

More from this blog

Shubham Tech. Blog's

55 posts

Problem Solver | Currently Working As a Full Stack Developer, Community Leader At @Dev_Matrix | Previously Contributor at @RealDevSquad, @TeamShiksha