Briefly describe the use of the test of method in regular expressions of JavaScript

  • 2020-06-15 07:46:20
  • OfStack

The test method searches for the text string for a regular expression match. If a match is found, true is returned; Otherwise return false.
grammar


RegExpObject.test( string );

Here is the details of the parameters:

string: The string to search for

The return value:

If 1 match is found, if not null, the matching text is returned.

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>

This will produce the following results:


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


Related articles: