English 中文(简体)
MooTools - Regular Expression
  • 时间:2024-09-17

MooTools - Regular Expression


Previous Page Next Page  

MooTools provides a way to create and use Regular Expression (regex). This tutorial will explain the basics and extreme uses of regexes.

Let us discuss a few methods of the regular expressions.

test()

test() is a method used to test the regular expression with the input string. While JavaScript already provides the RegExp object along with the test() function, MooTools adds more features to the RegExp object. Let us take an example and understand how to use the test() method. Take a look at the following code.

Example

<!DOCTYPE html>
<html>

   <head>
      <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script>
      <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script>
      
      <script type = "text/javascript">
         var regex_demo = function(){
            var test_string = $( regex_value ).get( value );
            var regex_value = $( regex_match ).get( value );
            var test_result = test_string.test(regex_value);
            
            if (test_result){
               $( regex_1_result ).set( html , "Matched");
            } else {
               $( regex_1_result ).set( html , "Not Match");
            }
         }
         
         window.addEvent( domready , function() {
            $( regex ).addEvent( cpck , regex_demo);
         });
      </script>
   </head>
   
   <body>
      String: <input type = "text" id = "regex_value"/><br/><br/>
      Reg Exp: <input type = "text" id = "regex_match"/><br/><br/>
      <input type = "button" id = "regex" value = "TEST"/><br/><br/>
      <Lable id = "regex_1_result"></Lable>
   </body>
   
</html>

You will receive the following output −

Output