Home > Web Front-end > JS Tutorial > A brief introduction to the use of the test() method in JavaScript regular expressions_Basic knowledge

A brief introduction to the use of the test() method in JavaScript regular expressions_Basic knowledge

WBOY
Release: 2016-05-16 15:54:44
Original
1807 people have browsed it

The test method is a text search string matched by a regular expression. Returns true if a match is found; false otherwise.
Grammar

RegExpObject.test( string );

Copy after login

Here are the details of the parameters:

  • string : The string to search for

Return value:

  • If a match is found, returns the matching text if not empty.

Example:

<html>
<head>
<title>JavaScript RegExp test Method</title>
</head>
<body>
<script type="text/javascript">
  var str = "Javascript is an interesting scripting language";
  var re = new RegExp( "script", "g" );

  var result = re.test(str);
  document.write("Test 1 - returned value : " + result); 

  re = new RegExp( "pushing", "g" );
  var result = re.test(str);
  document.write("<br />Test 2 - returned value : " + result); 
</script>
</body>
</html>

Copy after login

This will produce the following results:

Test 1 - returned value : true
Test 2 - returned value : false 

Copy after login

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