The test() method is used to perform a search for a match between a regular expression and a specified string. If there is a match, it returns true, if there is no match, it returns false. Next, let’s take a look at the test method. Specific use.
The search method we introduced in previous article is also used to search for matches between regular expressions and strings, so the following Let's take a look at how the test method searches for matches between regular expressions and strings.
Let’s first take a look at the basic syntax of the test method
RegExp.test( string );
RegExp represents the name of the regular expression.
string represents the string to be searched for
See specific examples
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> var str = "Javascript is an interesting scripting language"; var re = new RegExp( "script", "g" ); var result = re.test(str); document.write(result); re = new RegExp( "pushing", "g" ); var result = re.test(str); document.write("<br /> " + result); </script> </body> </html>
The results displayed on the browser are as follows: returned when there is a match true, returns false if there is no match.
#This article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the php Chinese website! ! !
The above is the detailed content of How to use test method. For more information, please follow other related articles on the PHP Chinese website!