RegExp (Regular Expressions)

RegExp (Regular Expressions): Regex Pattern Scan — Searching patterns in text Regular Expression (Regex) is a small but powerful language for searching specific patterns in text.

Regex Pattern Scan — Searching patterns in text

Regular Expression (Regex) is a small but powerful language for searching specific patterns in text. Think of it as Google's wildcard (*) search on steroids. In automation, we validate email formats, extract data from error messages, and check URL patterns. In Java we used `Pattern.compile()` and `Matcher`; JS's `/pattern/flags` syntax is far simpler. So with a tool this powerful available, why do experienced engineers think twice before reaching for it? Because regex trades readability for power: the 20-character email validation you write today becomes a wall nobody — including you — can read three months from now, and a wrong regex raises no error, it simply matches the wrong things. In a test that means `expect(text).toMatch(/error/)` passes against ANY text containing the word "error" on the page — including the sentence "no error found". The Java habit of compiling a `Pattern` once and holding it as a constant applies here too (creating a fresh regex literal inside a loop costs you performance in JS). The QA rule: prefer regex for extraction, not validation — answer "is this text correct?" with an exact comparison, and "pull the order number out of this text" with a regex.

JS Regex Methods — QA Examples

// ─── 1. test() — Boolean check ────────────────────────────── // Java: Pattern.compile(re).matcher(str).matches() const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; console.log(emailRegex.test('user@example.com')); // true console.log(emailRegex.test('invalid-email')); // false // ─── 2. match() — Returns matches as an array ───────────────── // Java: Matcher.group() const text = 'Error code: 404, redirect: 301'; const codes = text.match(/\d+/g); // 'g' flag: global, find all console.log('Found codes:', codes); // ['404', '301'] // ─── 3. replace() — Replace using regex ────────────────────── // Java: str.replaceAll(regex, replacement) const dirty = ' Hello World '; const clean = dirty.replace(/\s+/g, ' ').trim(); console.log('Clean:', clean); // 'Hello World' // ─── 4. QA — URL format validation ────────────────────────── const urlRegex = /^https?:\/\/[\w-]+(\.[\w-]+)+/; console.log(urlRegex.test('https://learnqa.dev')); // true console.log(urlRegex.test('ftp://invalid')); // false

Micro Lab: JavaScript QA coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: JavaScript QA coding practice

Read the goal and async dependencies

Place await / Promise chain in the right spot

Complete the assertion or expectation line

Run and read console or test output

If flaky, add a wait strategy or retry

What is the safe order for writing and testing JavaScript QA code?

🎬 Why Does the Same Regex Give a Different Result the Second Time? — The lastIndex Trap