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.

Answer from user113716 on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_match.asp
JavaScript String match() Method
❮ Previous JavaScript String Reference Next ❯ · A search for "ain" using a string: let text = "The rain in SPAIN stays mainly in the plain"; text.match("ain"); Try it Yourself » · A search for "ain" using a regular expression: let text = "The rain in SPAIN stays mainly in the plain"; text.match(/ain/); Try it Yourself » ·
Discussions

javascript - Check whether a string matches a regex in JS - Stack Overflow
The most significant difference between test and match (and matchAll) is that match does things like to return a list of all matching sub-strings, while test only checks if there are any. Check the regex methods in javascript.info/regexp-methods 2020-01-13T20:49:38.87Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
JavaScript - Use variable in string match - Stack Overflow
Exact duplicate of How do you pass ... JavaScript?. Search more ;) ... One more thing: If you are using a variable to construct an regexp, cares should be taken that the variable might contain regexp special characters. e.g. if you pass "c++", the regex compiler will complain SyntaxError: Invalid regular expression: /c++/: Nothing to repeat ... Although the match function doesn't accept string literals as ... More on stackoverflow.com
🌐 stackoverflow.com
How to use String.match() in javascript - Stack Overflow
I have this code here: Display the result here. ... More on stackoverflow.com
🌐 stackoverflow.com
[Javascript]String match with regex gives weird results
Read through the description section of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match - essentially .match() behaves differently if you use the /g flag. Also your use of ";?" at the end of the regex won't do anything, the "(.+)" before it will greedily match to the end of the string. I'm curious what you're trying to do here, and why we're parsing JavaScript commands in a regular expression in the first place. More on reddit.com
🌐 r/learnprogramming
7
2
October 12, 2015
🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-match-method-ce47295bfd97
Basics of Javascript · String · match() (method) | by Jakub Korch | Nerd For Tech | Medium
June 9, 2021 - The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-match-method
JavaScript String match() Method - GeeksforGeeks
July 11, 2025 - The match() method in JavaScript is used for identifying and retrieving substrings that fit a specified pattern, defined by a regular expression. It is often used when you need to find particular patterns within strings, enabling efficient text ...
Find elsewhere
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript | MDN
When the search for a match requires something more than a direct match, such as finding one or more b's, or finding white space, you can include special characters in the pattern. For example, to match a single "a" followed by zero or more "b"s followed by "c", you'd use the pattern /ab*c/: the * after "b" means "0 or more occurrences of the preceding item." In the string "cbbabbbbcdebc", this pattern will match the substring "abbbbc".
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
Returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order. ... Used to match regular expression regexp against a string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-match-multiple-parts-of-a-string-using-regexp-in-javascript
JavaScript - How to Match Multiple Parts of a String Using RegExp? - GeeksforGeeks
July 23, 2025 - In JavaScript, the regular expression (RegExp) object provides a powerful mechanism to search, match, and manipulate strings. One of the common use cases is matching multiple parts of a string that satisfy a certain pattern.
🌐
W3Schools
w3schools.com › js › js_string_search.asp
JavaScript String Search
The match() method returns an array containing the results of matching a string against a string (or a regular expression).
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › match
JavaScript String Match() - Match String Pattern | Vultr Docs
November 15, 2024 - This regular expression checks whether the string starts with one or more word characters, followed by an @, more word characters, a dot, and ends with one or more word characters, thus validating a basic email format.
🌐
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details.
🌐
CodingNomads
codingnomads.com › match-string-javascript
JavaScript String Matching
Learn string matching in JavaScript and how to use regex for complex patterns, from checking substrings to extracting emails.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript string match
JavaScript String Match
September 1, 2008 - If the match is found in the original string, then it returns an array with matches. In the following example, we are using the JavaScript String match() method to search for the string 'h' and retrieve an array with all matches in the original string "Hi how are you?"
🌐
Programiz
programiz.com › javascript › library › string › match
Javascript String match()
Become a certified JavaScript programmer. Try Programiz PRO! ... The match() method returns the result of matching a string against a regular expression.
🌐
Can I Use
caniuse.com › mdn-javascript_builtins_string_match
JavaScript built-in: String: match | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
Reddit
reddit.com › r/learnprogramming › [javascript]string match with regex gives weird results
r/learnprogramming on Reddit: [Javascript]String match with regex gives weird results
October 12, 2015 -

As I understand it, .match is supposed to return only matched groups, and would be the thing to use for "global" matches, so I'm doing just that to get variable names and their definitions from the code of a function. However...

var str = "function startMidiMusic(s) {	popBox = window.open(s,'midi','scrollbars=no,resizable=no,width=250,height=125,left=10,top=10');	popBox.focus();	foo = 'bar';}";

//Gets an extraneous thing and only the first result
str.match(/(\S+) ?= ?(.+);?/);
>> Array [ "popBox = window.open(s,'midi','scro…", "popBox", "window.open(s,'midi','scrollbars=no…" ]

//Only gets a portion of the "original" string
str.match(/(\S+) ?= ?(.+);?/g);
>> Array [ "popBox = window.open(s,'midi','scro…" ]

None of them include "foo" and "bar", let alone in the way I'm expecting

What's going on?

Top answer
1 of 2
1
Read through the description section of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match - essentially .match() behaves differently if you use the /g flag. Also your use of ";?" at the end of the regex won't do anything, the "(.+)" before it will greedily match to the end of the string. I'm curious what you're trying to do here, and why we're parsing JavaScript commands in a regular expression in the first place.
2 of 2
1
Well, the first thing of note is that your regex doesn't quite seem correct. Assuming you wanted to capture the name of the variable being assigned to and the value it's given, we can't use .+, since that would consume the ; that marks the end of the value (and thus we'd end up consuming too much) Instead, we'd have to use a group containing all valid characters we'd want to consume (which is pretty much everything not a ;). /(\S+) ?= ?([^;]+);?/ An alternative would be to use lazy matching for something that must end with a semicolon: /(\S+) ?= ?(.+?;)/ The laziness there is crucial. It ensures it tries to match as little as possible, and thus stops matching at that first semicolon. Of course, both approaches still fail if there's a semicolon inside a string. You can't parse JS with regex! But I'll assume you're aware of this limitation and are okay with the approximate results. In this case, the returned value is: The match made: "popBox = window.open(s,'midi','scrollbars=no,resizable=no,width=250,height=125,left=10,top=10');" The two capture groups: "popBox" and ""window.open(s,'midi','scrollbars=no,resizable=no,width=250,height=125,left=10,top=10')". In case it wasn't obvious, the … is the browser's console truncating a long string. Access the array element itself to see the full string. That's presumably why you think you're only getting a portion of the original string. You actually got it all, but it wasn't all shown.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-string-match
A Quick Guide to the String Match Method in JavaScript | DigitalOcean
January 7, 2020 - The string match() method will return an array with the items being matches from a provided regular expression found in the string. You can read more about regular expressions in JavaScript here.
🌐
JavaScript.info
javascript.info › tutorial › regular expressions
Methods of RegExp and String
July 13, 2022 - The method str.match(regexp) finds matches for regexp in the string str.