Home  >  Article  >  Web Front-end  >  How to use test method

How to use test method

不言
不言Original
2019-01-31 14:39:1710251browse

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.

How to use test method

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.

How to use test method

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn