Use regex.test() if all you want is a boolean result:
console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false
console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true
console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true
...and you could remove the () from your regexp since you've no need for a capture.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › RegExp › test
RegExp.prototype.test() - JavaScript | MDN
The test() method of RegExp instances executes a search with this regular expression for a match between a regular expression and a specified string. Returns true if there is a match; false otherwise.
Regex101
regex101.com
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
Regular Expressions test() - match()? Are these used interchangeably?
Jonathan Grieve is having issues with: Just a quick question to help clear up some confusion. Joel mentions the match() method but seems to use the test() method in its place.... More on teamtreehouse.com
Extract Matches VS Test
Tell us what’s happening: I’m curious why when you use test() method, you use the the method as regex.test(str) let regex = /Code/; let result = regex.test(str); When you use match() method, you use the method as str.match(regex) let regex= /Code/; let result = str.match(regex); The only ... More on forum.freecodecamp.org
RegEx test method (Javascript)
Whelp, this is not a regex problem, but a Javascript problem. What's happening here is that when you're using the g flag, the regex pattern is "stateful", meaning that it remembers where it left off in the expression when it starts testing another expression. The docs say ... JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g. /foo/g or /foo/y). They store a lastIndex from the previous match. To fix this, you can either reset the lastIndex or do not use the g flag. Here is a great article that explains it way better than I could More on reddit.com
Test regEx in Javascript.
For the second string, the Regex is actually matching against the middle of the string. Check this out on Regex101 . Specifically, the NPAFRPOX part of BNPAFRPOXXX is matching. You have NPAFRP for 6 capital letters. [A-Z]{6,6} O for a capital letter or number > 1 [A-Z2-9] X for a [A-NP-Z0-9] an alphanumeric besides O And ([A-Z0-9]{3,3}){0,1} is ignored by choosing the 0 option. Do note that, by default, Regex patterns will generally try for any possible match, even if it's not the one you expect. This is why you must add extra control characters and flags to ensure that unexpected behavior doesn't occur. To fix this regex, you might want to add boundary characters ^$ to ensure that the entire string must match, rather than just a substring. So: ^[A-Z]{6,6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3,3}){0,1}$ If you're instead looking for these codes as part of a larger string, perhaps use the word boundary character \b instead. If you can't rely upon word boundaries, you'll have to find a different reliably condition to separate things which are codes from things which are parts of code/noise. Good luck! More on reddit.com
Videos
02:51
JavaScript Regular Expressions: Using the Test Method | FreeCodeCamp ...
01:00
How to use regex to check if a javascript string contains a pattern ...
03:40
JavaScript Regex: Mastering the test() Method for Beginners - YouTube
12:22
Regular Expressions (RegEx) Tutorial #13 - Testing a RegEx Pattern ...
08:27
01 Using the Test Method - RegEx - FreeCodeCamp ...
06:28
JavaScript Regex for Absolute Beginners | Beginner Guide - YouTube
Top answer 1 of 15
1847
Use regex.test() if all you want is a boolean result:
console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false
console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true
console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true
...and you could remove the () from your regexp since you've no need for a capture.
2 of 15
271
Use test() method :
var term = "sample1";
var re = new RegExp("^([a-z0-9]{5,})$");
if (re.test(term)) {
console.log("Valid");
} else {
console.log("Invalid");
}
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
Regular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference, and searchable community patterns.
W3Schools
w3schools.com › jsref › jsref_regexp_test.asp
W3Schools.com
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion · ❮ Previous JavaScript RegExp Methods Next ❯ · Search a string for the character "e": let text = "The best things in life are free"; let pattern = /e/; let result = pattern.test(text); Try it Yourself » ·
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript | MDN
An online tool to learn, build, & test Regular Expressions. ... An online interactive tutorials, Cheat sheet, & Playground. ... An online visual regex tester.
Regex Tester
regextester.com
Regex Tester and Debugger Online - Javascript, PCRE, PHP
Regular Expression Tester with highlighting for Javascript and PCRE. Quickly test and debug your regex.
Debuggex
debuggex.com
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.
Test your regex by visualizing it with a live editor. JavaScript, Python, and PCRE.
Softchris
softchris.github.io › pages › javascript-regex.html
Regex in JS - how YOU can learn it and learn to like it
Or like this where we create an ... for a match in a string. It returns an array of information or null on a mismatch. test(), tests for a match in string, answers with true or false...
Honeybadger
honeybadger.io › blog › javascript-regular-expressions
The ultimate JavaScript regex guide - Honeybadger Developer Blog
May 9, 2025 - Note: You don't need to add forward slashes to the pattern when using the RegExp constructor to create patterns, but you do need to enclose them in either single or double quotes. There are different ways to test regex patterns in JavaScript, depending on how you want the result returned.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › match
String.prototype.match() - JavaScript | MDN
If you need to know if a string matches a regular expression RegExp, use RegExp.prototype.test().
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › RegExp
RegExp - JavaScript | MDN
A string that contains the flags of the RegExp object. ... Whether to test the regular expression against all possible matches in a string, or only against the first.
Qodex
qodex.ai › home › all tools › getting started › javascript regex tester
JavaScript Regex Tester — Test Patterns Live Online
Test and debug JavaScript regex patterns in real time. See matches highlighted, capture groups extracted, and flags applied instantly. Free, no signup needed.
Trackingplan
trackingplan.com › regex-tester
Regex Tester Online: Test & Validate Regular Expressions | Trackingplan
Test and validate your regular expressions with our Online Regex Tester. Supports JavaScript, TypeScript, and more. Try our Regex Checker and Validator.
Regex Pal
regexpal.com
Regex Tester - Javascript, PCRE, PHP
Test your Javascript and PCRE regular expressions online.
Reddit
reddit.com › r/regex › regex test method (javascript)
r/regex on Reddit: RegEx test method (Javascript)
July 30, 2021 -
I'm new to RegEx, I ran the test regex method twice on the same string and it returns true for the first test and false for the second, why is that happening ? what is the difference between the two tests ?