🌐
W3Schools
w3schools.com › jsref › jsref_match.asp
JavaScript String match() Method
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion · ❮ 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 » ·
🌐
W3Schools
w3schools.com › js › js_string_search.asp
W3Schools.com
The match() method returns an array containing the results of matching a string against a string (or a regular expression).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › match
String.prototype.match() - JavaScript | MDN
const str1 = "All numbers except NaN satisfy <= Infinity and >= -Infinity in JavaScript."; const str2 = "My grandfather is 65 years old and My grandmother is 63 years old."; const str3 = "The contract was declared null and void."; str1.match("number"); // "number" is a string.
🌐
W3Schools Blog
w3schools.blog › home › javascript string match() method
JavaScript String match() method - W3schools
June 1, 2019 - <!DOCTYPE html> <html> <body> <script> var a ="Best tutorial website: w3schools"; document.write(a.match('co')); document.write("</br>"); document.writeln(a.match(/web/)); document.write("</br>"); document.writeln(a.match(/web/i)); </script> </body> </html>
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-string-match
A Quick Guide to the String Match Method in JavaScript | DigitalOcean
January 7, 2020 - In as few words as possible, this post will teach you how the JavaScript match method works to retrieve regular expression match(es) from within a string.
🌐
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".
🌐
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.
🌐
TechOnTheNet
techonthenet.com › js › string_match.php
JavaScript: String match() method
This JavaScript tutorial explains how to use the string method called match() with syntax and examples. In JavaScript, match() is a string method that is used to find matches based on regular expression matching.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › string-match
The String `match()` Function in JavaScript - Mastering JS
JavaScript Strings have a match() method that returns an array if the string matches a given regular expression, or null otherwise.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › javascript › strings › .match()
JavaScript | Strings | .match() | Codecademy
June 11, 2025 - The .match() method searches a string for matches against a regular expression and returns the result as an array object. The .match() method is used in JavaScript to find parts of a string that match a regular expression.
🌐
Programiz
programiz.com › javascript › library › string › match
Javascript String match()
The match() method returns the result of matching a string against a regular expression.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-match-method
JavaScript String match() Method - GeeksforGeeks
July 11, 2025 - When both the global (g) and case-insensitive (i) flags are used together, the match() method will find all case-insensitive matches throughout the string.
🌐
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.
🌐
W3Schools
w3schoolsua.github.io › js › js_string_search_en.html
JavaScript String Search. Lessons for beginners. W3Schools in English
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object. ... Read more about regular expressions in the chapter JS RegExp.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript string match
JavaScript String Match
September 1, 2008 - The following is another example of the JavaScript match() method. Here, we use this method to search for the regular expression '/src/g' globally in the original string 'JavaScript is a scripting language' and try to retrieve an array with matches.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-regex-match-example-how-to-use-the-js-replace-method-on-a-string
JavaScript Regex Match Example – How to Use JS Replace on a String
January 4, 2021 - If you'd like all instances of the word "we" whether it's upper or lowercase, you have a couple of options. First, you could use the .toLowercase() method on the string before testing it with the .match() method:
Top answer
1 of 2
5

Global Variable Scope

It is because you are using name as your variable. This is a global browser window variable that is inherently a string and cannot be stored as an Array

Even if you redeclare it with var name =, you are still in the global scope. And thus name (aka window.name) simply retains the last value you assign to it.

You can test this with the following on an empty page without defining any variables at all:

console.log(name===window.name) // Returns true
console.log(name,window.name)   // Returns 'Safari Safari' for my browser

Change name to something else

If you change your name variable to simply have a different name, like my_name, it stores the result of .match() as an Array.

var my_name = str.match(/[^\/]+/i);
var version = str.match(/[0-9|\.]+/i);

console.log(typeof my_name, my_name instanceof Array) // Returns object, true

Change Scope by wrapping in a function

This is your exact code wrapped inside a function and returns the correct variable types:

function getBrowserStuff(){

    var userAgent = navigator.userAgent;
    var splitted = userAgent.match(/[(][^)]+[)]|\w+\/\S+/ig);

    for (var i = 0; i < splitted.length; i++ ) {
        var str = splitted[i];
        if (str[0] == '(') {
        } else {
            var name = str.match(/[^\/]+/i);
            var version = str.match(/[0-9|\.]+/i);
            console.log('Name','Typeof '+(typeof name), 'IsArray '+(name instanceof Array),name)
            console.log('Version','Typeof '+(typeof version),'IsArray '+(version instanceof Array),version)
        }
    }

    return 'whatever'
}

getBrowserStuff()

Changing the variable name to my_name OR wrapping code like the above function returns this:

Name    Typeof object IsArray true ["Mozilla"]
Version Typeof object IsArray true ["5.0"]
Name    Typeof object IsArray true ["AppleWebKit"]
Version Typeof object IsArray true ["600.3.18"]
Name    Typeof object IsArray true ["Version"]
Version Typeof object IsArray true ["8.0.3"]
Name    Typeof object IsArray true ["Safari"]
Version Typeof object IsArray true ["600.3.18"]

Where before it returned this:

Name    Typeof string IsArray false Mozilla
Version Typeof object IsArray true  ["5.0"]
Name    Typeof string IsArray false AppleWebKit
Version Typeof object IsArray true  ["600.3.18"]
Name    Typeof string IsArray false Version
Version Typeof object IsArray true  ["8.0.3"]
Name    Typeof string IsArray false Safari
Version Typeof object IsArray true  ["600.3.18"]
2 of 2
0

This is not possible, or is a bug of your implementation.

According to the ECMAScript 5.1 specification, match behaves like this:

15.5.4.10 String.prototype.match (regexp)

When the match method is called with argument regexp, the following steps are taken:

  1. Call CheckObjectCoercible passing the this value as its argument.
  2. Let S be the result of calling ToString, giving it the this value as its argument.
  3. If Type(regexp) is Object and the value of the [[Class]] internal property of regexp is "RegExp", then let rx be regexp;
  4. Else, let rx be a new RegExp object created as if by the expression new RegExp(regexp) where RegExp is the standard built-in constructor with that name.
  5. Let global be the result of calling the [[Get]] internal method of rx with argument "global".
  6. Let exec be the standard built-in function RegExp.prototype.exec (see 15.10.6.2)
  7. If global is not true, then
    1. Return the result of calling the [[Call]] internal method of exec with rx as the this value and argument list containing S.
  8. Else, global is true
    1. Call the [[Put]] internal method of rx with arguments "lastIndex" and 0.
    2. Let A be a new array created as if by the expression new Array() where Array is the standard built-in constructor with that name.
    3. Let previousLastIndex be 0.
    4. Let n be 0.
    5. Let lastMatch be true.
    6. Repeat, while lastMatch is true
      1. Let result be the result of calling the [[Call]] internal method of exec with rx as the this value and argument list containing S.
      2. If result is null, then set lastMatch to false.
      3. Else, result is not null
        1. Let thisIndex be the result of calling the [[Get]] internal method of rx with argument "lastIndex".
        2. If thisIndex = previousLastIndex then
          1. Call the [[Put]] internal method of rx with arguments "lastIndex" and thisIndex+1.
          2. Set previousLastIndex to thisIndex+1.
        3. Else, set previousLastIndex to thisIndex.
        4. Let matchStr be the result of calling the [[Get]] internal method of result with argument "0".
        5. Call the [[DefineOwnProperty]] internal method of A with arguments ToString(n), the Property Descriptor {[[Value]]: matchStr, [[Writable]]: true, [[Enumerable]]: true, [[configurable]]: true}, and false.
        6. Increment n.
    7. If n = 0, then return null.
    8. Return A.

Therefore, for your global regex, the only possible returned values are null or A, which is an array.

For the non global ones, the result of calling RegExp.prototype.exec is returned. But it also returns an array or null:

Performs a regular expression match of string against the regular expression and returns an Array object containing the results of the match, or null if string did not match.