Skip to content

Latest commit

 

History

History
79 lines (64 loc) · 2.33 KB

File metadata and controls

79 lines (64 loc) · 2.33 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
test Method (Regular Expression) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
test
JavaScript
TypeScript
DHTML
test method
4f4b6e39-cb1a-4be9-a66f-7b846075580d
13
mikejo5000
mikejo
ghogen

test Method (Regular Expression) (JavaScript)

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

Syntax

  
rgExp.test(str)   

Parameters

rgExp
Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags.

str
Required. The string on which to perform the search.

Remarks

The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise.

The properties of the global RegExp object are not modified by the test method.

Example

The following example illustrates the use of the test method. To use this example, pass the function a regular expression pattern and a string. The function will test for the occurrence of the regular expression pattern in the string and return a string indicating the results of that search:

function TestDemo(re, teststring)  
{  
   // Test string for existence of regular expression.  
   var found = re.test(teststring)  
  
   // Format the output.  
   var s = "";  
   s += "'" + teststring + "'"  
  
   if (found)  
      s += " contains ";  
   else  
      s += " does not contain ";  
  
   s += "'" + re.source + "'"  
   return(s);  
}  

Requirements

[!INCLUDEjsv3]

Applies To: Regular Expression Object

See Also

RegExp Object
Regular Expression Object
Regular Expression Syntax (JavaScript)