Home > Web Front-end > JS Tutorial > How to use test method

How to use test method

不言
Release: 2019-01-31 14:39:17
Original
10300 people have browsed it

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 );
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template