JavaScriptConcepts

← Back to Home

JavaScript Regular Expressions —

Covers: syntax, flags, character classes, quantifiers, groups, assertions, Unicode, APIs, performance, pitfalls, debugging, common patterns, and interview questions.


1. Basics — what a RegExp is

A RegExp is a pattern describing a set of strings. Two creation forms:

const r1 = /abc/i;            // literal form, flags after closing slash
const r2 = new RegExp("a\\d+", "g"); // constructor form; note double-escaping in string

/…/ is parsed at script-compile time; RegExp() builds at runtime.


2. Flags (modifiers)

Examples:

/^\w+/m      // multiline start-of-line word
/./s         // dotAll: dot matches \n
/\p{Letter}/u // unicode property escape

3. Literal vs RegExp constructor differences


4. Character classes & shorthand


5. Unicode and property escapes


6. Quantifiers


7. Grouping & capture


8. Assertions (lookaround)


9. Anchors


10. Common methods / RegExp API


11. lastIndex, g, y, and exec behavior

Pitfall:

const re = /a/g;
re.test("a"); // true, lastIndex becomes 1
re.test("a"); // false, since lastIndex=1 and search starts after end

12. Escaping rules / special chars

Characters with special regex meaning must be escaped: .^$*+?()[]\{}|/

function escapeRE(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }

13. Greedy vs lazy vs catastrophic backtracking


14. Performance tips


15. Useful patterns & examples

Email (simple, pragmatic)

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

URL (very simple)

/^(https?:\/\/)?([\w.-]+)\.([a-z]{2,})(\/\S*)?$/i

Extract all words (Unicode-aware)

const words = [...text.matchAll(/\p{L}[\p{L}\p{N}_']*/gu)];

Capture duplicate adjacent words (case-insensitive)

/\b(\w+)\s+\1\b/i

Validate hex color

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/

Split on commas not inside quotes

const parts = str.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/);

Remove HTML tags (simple)

str.replace(/<[^>]*>/g, "");

16. Debugging & testing tools (workflow)


17. Common pitfalls


18. Advanced topics (brief)


19. Quick reference cheat-sheet


20. Replacement template tokens

Inside replacement string:

Example:

"John".replace(/(J)(ohn)/, "$2, $1"); // "ohn, J"

21. Interview questions (regex-focused)

  1. Explain differences between g and y flags.
  2. How does lastIndex work and what pitfalls does it cause with test() and exec()?
  3. Why use non-capturing groups (?: … )?
  4. How to safely build a regex from user input? (Show escape function.)
  5. Explain greedy vs lazy quantifiers and give examples.
  6. What is catastrophic backtracking and how to mitigate it?
  7. How do lookaheads differ from lookbehinds; give use-cases for each.
  8. How to match full Unicode letters in JS reliably? (Use u + \p{Letter} or Intl.Segmenter.)
  9. Why is new RegExp(".") different from /./s sometimes? (String escaping & flags.)
  10. How to capture replacement groups with names and use them in .replace().

22. Short sample patterns with explanation

Phone with optional country code

/^(?:\+?(\d{1,3}))?[-. (]?(\d{3})[-. )]?(\d{3})[-. ]?(\d{4})$/

CSV field split respecting quotes

/(?:^|,)(?:"([^"]*(?:""[^"]*)*)"|([^",]*))/g