• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • test()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. escape()
  • Static properties
    1. $1, …, $9 Deprecated
    2. input ($_) Deprecated
    3. lastMatch ($&) Deprecated
    4. lastParen ($+) Deprecated
    5. leftContext ($`) Deprecated
    6. rightContext ($') Deprecated
    7. compile() Deprecated
    8. [Symbol.match]()
    9. [Symbol.split]()
  • Instance properties
    1. global
    2. source
    3. Object/Function
    4. Static methods
      1. toString()
      2. displayName Non-standard
      3. arguments Non-standard Deprecated
      4. caller Non-standard Deprecated
    5. Instance methods
      1. __defineGetter__() Deprecated
      2. __defineSetter__() Deprecated
      3. __lookupGetter__() Deprecated
      4. __lookupSetter__() Deprecated
      5. toLocaleString()
      6. __proto__ Deprecated
      7. 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.

        JavaScript RegExp objects are stateful when they have the sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).

  • Try it

    const str = "table football";
    
    const regex = /fo+/;
    const globalRegex = /fo+/g;
    
    console.log(regex.test(str));
    / Expected output: true
    
    console.log(globalRegex.lastIndex);
    / Expected output: 0
    
    console.log(globalRegex.test(str));
    / Expected output: true
    
    console.log(globalRegex.lastIndex);
    / Expected output: 9
    
    console.log(globalRegex.test(str));
    / Expected output: false
    

    Syntax

    js
    test(str)
    

    Parameters

    str

    The string against which to match the regular expression. All values are coerced to strings, so omitting it or passing undefined causes test() to search for the string "undefined", which is rarely what you want.

    Return value

    true if there is a match between the regular expression and the string str. Otherwise, false.

    Description

    Use test() whenever you want to know whether a pattern is found in a string. test() returns a boolean, unlike the String.prototype.search() method (which returns the index of a match, or -1 if not found).

    To get more information (but with slower execution), use the exec() method. (This is similar to the String.prototype.match() method.)

    As with exec() (or in combination with it), test() called multiple times on the same global regular expression instance will advance past the previous match.

    Examples

    Using test()

    This example tests if "hello" is contained at the very beginning of a string, returning a boolean result.

    js
    const str = "hello world!";
    const result = /^hello/.test(str);
    
    console.log(result); / true
    

    The following example logs a message which depends on the success of the test:

    js
    function testInput(re, str) {
      const midString = re.test(str) ? "contains" : "does not contain";
      console.log(`${str} ${midString} ${re.source}`);
    }
    

    Using test() on a regex with the "global" flag

    When a regex has the global flag set, test() will advance the lastIndex of the regex. (RegExp.prototype.exec() also advances the lastIndex property.)

    Further calls to test(str) will resume searching str starting from lastIndex. The lastIndex property will continue to increase each time test() returns true.

    Note: As long as test() returns true, lastIndex will not reset—even when testing a different string!

    When test() returns false, the calling regex's lastIndex property will reset to 0.

    The following example demonstrates this behavior:

    js
    const regex = /foo/g; / the "global" flag is set
    
    / regex.lastIndex is at 0
    regex.test("foo"); / true
    
    / regex.lastIndex is now at 3
    regex.test("foo"); / false
    
    / regex.lastIndex is at 0
    regex.test("barfoo"); / true
    
    / regex.lastIndex is at 6
    regex.test("foobar"); / false
    
    / regex.lastIndex is at 0
    regex.test("foobarfoo"); / true
    
    / regex.lastIndex is at 3
    regex.test("foobarfoo"); / true
    
    / regex.lastIndex is at 9
    regex.test("foobarfoo"); / false
    
    / regex.lastIndex is at 0
    / (...and so on)
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-regexp.prototype.test

    Browser compatibility

    See also

    • Regular expressions guide
    • RegExp

    Help improve MDN

    RegExp object represents a regular expression, a notation for matching text patterns.","name":"Regular expressions"}},"browserCompat":["javascript.builtins.RegExp.test"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant