RegExp.test()

JavaScript 1.2+, JScript 3.0+ Nav4+, NES3+, IE4+ Syntax

							regexp.test()

Description

The test() method of the RegExp object is used to test for a pattern match in a string. Returns Boolean value true or false.

Example

Listing 6.226 shows how the test() method is used.

Listing 6.226 Example of test() Method
 <html> <body> <script language="JavaScript"> <!–– Hide // create a new regular expression myExp = new RegExp("hope", "g"); // define a string object str = "I hope everything is going well."; // test to see if the regular expression exists in the string if(myExp.test(str)){ document.write("The test found \"hope\" in the string: " + "<b>" + " I hope everything is going well" + "</b>"); } // End Hide ––> </script> </body> ...

Get Pure JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.