JavaScript RegExp.test() validator
Trace why repeated checks can return different results, then use the examples below to build a full-value validator.
Trace RegExp.test() and lastIndex
See what JavaScript returns when it reuses the same regex across calls. Run the example, then reset the index before each call to compare. Your pattern and text stay in this browser.
Why the same pattern can return true, then false
A global (g) expression searches from lastIndex. A sticky (y) expression must match at that exact offset. On success either flag moves the index to the end of the match, even if the next call uses a different string. On failure either flag resets the index to zero. A zero-length match can leave the index unchanged.
For independent yes/no checks, leave off g and y unless you need their behavior. If you need the original flags, set lastIndex to zero before testing each value. To see the difference, try foo with y on a line that starts with a space, then try g.
How to check a regex.test validator
- Write the validator rule as a true or false decision before editing the expression.
- Anchor the regex with `^` and `$` when `regex.test(value)` must approve the entire value.
- Paste passing and failing values on separate lines, then use `m` only when each line is an independent value.
- Retest the same value twice if your code uses `g` or `y`, because global and sticky regex objects can carry `lastIndex` state.
- Copy the final expression into code with a short test table of accepted and rejected values.
Boolean validator expressions to test
Ticket code
^[A-Z]{2}-\d{4}$
Pass: AB-2048, ZX-7712
Fail: ab-2048, AB-20, ID: AB-2048
Useful for showing why `.test()` validators need anchors. Without anchors, a valid-looking fragment inside a longer string can pass.
Lowercase slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
Pass: regex-test, validator-2026
Fail: Regex-Test, regex--test, regex-test-
Checks the whole slug and rejects doubled or trailing separators before route code sees the value.
Boolean yes or no
^(?:yes|no)$
Pass: yes, no
Fail: y, nope, yes please
A small example where substring matching would be too loose for application logic.
Environment key
^[A-Z][A-Z0-9_]{2,39}$
Pass: API_TOKEN, NEXT_PUBLIC_SITE_URL
Fail: _TOKEN, api_token, TOKEN-NAME
Keeps uppercase config keys predictable while still allowing digits and underscores.
JavaScript test-method details to verify
- `regex.test(value)` answers whether a match exists; it does not automatically mean the whole value is valid.
- For full-field validation, build the expression around the field boundary instead of checking a fragment inside the field.
- Avoid reusing a global or sticky `RegExp` object for repeated yes/no validation unless you reset `lastIndex` first.
- Use `String.match()` when you need returned groups. Use `.test()` when the code only needs a boolean.
- Keep UI trimming, lowercase conversion, and normalization consistent between the online tester and production code.
Copy-ready regex.test examples
Safe full-value check
const ticketPattern = /^[A-Z]{2}-\d{4}$/;
const isTicketCode = ticketPattern.test(value);The anchors make the boolean result mean the entire value is valid.
A reused global pattern
const reused = /foo/g;
reused.test('foo'); // true
reused.test('foo'); // false, then lastIndex resets to 0Use a non-global regex for simple validators, or create/reset the regex before repeated `.test()` calls.
Test a list of samples
const samples = ['AB-2048', 'ab-2048', 'ID: AB-2048'];
const results = samples.map((value) => [value, /^[A-Z]{2}-\d{4}$/.test(value)]);A small sample table catches both false positives and false negatives before the expression ships.
Check a regex.test expression now
Open the live tester with a ticket-code validator and pass/fail samples already loaded. The failing row ID: AB-2048 is there to prove the expression rejects partial matches.
Related Pages on Regex Tester
Open the live regex.test checker
Load a JavaScript expression, flags, and samples into the browser tester.
JavaScript regex validator
Build full-field validation expressions with anchors, pass samples, and fail samples.
JavaScript regex testing workflow
Check broader RegExp behavior, flags, capture groups, replacements, and repeated tests.
JavaScript regex flags tester
Confirm when `g`, `m`, `u`, and `y` change validator behavior.
JavaScript multiline validator
Validate one value per line without confusing line anchors and string anchors.
JavaScript regex examples
Copy practical JavaScript expressions after checking their boolean behavior.
Regex cheat sheet
Reference anchors, groups, alternation, quantifiers, and character classes.
JavaScript regex.test Validator FAQ
What does regex.test do in JavaScript?
`regex.test(value)` returns a boolean that says whether the regular expression found a match in the value. For validation, add anchors when the whole value must match.
Why can regex.test pass an invalid value?
The most common reason is a partial match. If the expression is not anchored, `.test()` can return true because one substring is valid even though the complete input is not.
Should validators use the g flag with regex.test?
Usually no. A global regex changes `lastIndex` between calls, so repeated `.test()` checks can return different results. Use a non-global regex for simple boolean validators.