MooTools - Espressione regolare

MooTools fornisce un modo per creare e utilizzare Regular Expression (regex). Questo tutorial spiegherà le basi e gli usi estremi delle regex.

Discutiamo alcuni metodi delle espressioni regolari.

test()

test () è un metodo utilizzato per testare l'espressione regolare con la stringa di input. Sebbene JavaScript fornisca già l'oggetto RegExp insieme alla funzione test (), MooTools aggiunge ulteriori funzionalità all'oggetto RegExp. Facciamo un esempio e capiamo come utilizzare il metodo test (). Dai un'occhiata al seguente codice.

Esempio

<!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('click', 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>

Riceverai il seguente output:

Produzione

Ignora maiuscole / minuscole

Questa è una delle situazioni importanti nel concetto di espressioni regolari. Se non vuoi che un'espressione regolare faccia distinzione tra maiuscole e minuscole, chiama il metodo di test con un'opzione 'I'. Facciamo un esempio che spieghi il caso ignora nell'espressione regolare. Dai un'occhiata al seguente codice.

Esempio

<!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, "i");
            
            if (test_result){
               $('regex_1_result').set('html', "Matched");
            } else {
               $('regex_1_result').set('html', "Not Match");
            }
         }
         
         window.addEvent('domready', function() {
            $('regex').addEvent('click', 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>

Riceverai il seguente output:

Produzione

Regex inizia con "^"

L'espressione regolare '^' (cap) è un operatore speciale che ti permette di controllare l'espressione regolare all'inizio di una data stringa. Questo operatore viene utilizzato come prefisso all'espressione regolare. Facciamo un esempio che spieghi come utilizzare questo operatore. Dai un'occhiata al seguente codice.

Esempio

<!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('click', 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 = "Match"/><br/><br/>
      <Lable id = "regex_1_result"></Lable>
   </body>
   
</html>

Riceverai il seguente output:

Produzione

Regex termina con "$"

Il Regex '$' (dollaro) è un operatore speciale che ti permette di controllare l'espressione regolare alla fine di una data stringa. Questo operatore viene utilizzato come suffisso per l'espressione regolare. Facciamo un esempio che spieghi come utilizzare questo operatore. Dai un'occhiata al seguente codice.

Esempio

<!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('click', 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 = "Match"/><br/><br/>
      <Lable id = "regex_1_result"></Lable>
   </body>
   
</html>

Riceverai il seguente output:

Produzione

Classi di caratteri

Le classi di caratteri sono una fase delle espressioni regolari che consentono di abbinare caratteri specifici (A o Z) o intervalli di caratteri (A - Z). Ad esempio, se vuoi verificare se una delle parole foo e zoo esiste in una stringa, le classi ti permettono di farlo inserendo i caratteri nelle parentesi quadre [] con le espressioni regolari. Dai un'occhiata al seguente codice.

Esempio

<!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_1 = function(){
            var test_string = $('regex_value').get('value');
            var regex_value = $('regex_match_1').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");
            }
         }
         
         var regex_demo_2 = function(){
            var test_string = $('regex_value').get('value');
            var regex_value = $('regex_match_2').get('value');
            var test_result = test_string.test(regex_value);
            
            if (test_result){
               $('regex_2_result').set('html', "Matched");
            } else {
               $('regex_2_result').set('html', "Not Match");
            }
         }
         
         var regex_demo_3 = function(){
            var test_string = $('regex_value').get('value');
            var regex_value = $('regex_match_3').get('value');
            var test_result = test_string.test(regex_value);
            
            if (test_result){
               $('regex_3_result').set('html', "Matched");
            } else {
               $('regex_3_result').set('html', "Not Match");
            }
         }
         
         window.addEvent('domready', function() {
            $('regex_1').addEvent('click', regex_demo_1);
            $('regex_2').addEvent('click', regex_demo_2);
            $('regex_3').addEvent('click', regex_demo_3);
         });
      </script>
   </head>
   
   <body>
      String: <input type = "text" id = "regex_value"/><br/><br/>
      Reg Exp 1: <input type = "text" id = "regex_match_1"/> 
      <input type = "button" id = "regex_1" value = "Match"/> 
      <Lable id = "regex_1_result"></Lable><br/><br/>
      
      Reg Exp 2: <input type = "text" id = "regex_match_2"/> 
      <input type = "button" id = "regex_2" value = "Match"/> 
      <Lable id = "regex_2_result"></Lable><br/><br/>
      
      Reg Exp 3: <input type = "text" id = "regex_match_3"/> 
      <input type = "button" id = "regex_3" value = "Match"/> 
      <Lable id = "regex_3_result"></Lable>
   </body>
   
</html>

Riceverai il seguente output:

Produzione

escapeRegExp ()

Questo metodo viene utilizzato per ignorare i caratteri di escape da una determinata stringa durante il controllo con un'espressione regolare. Di solito, i caratteri di escape sono:

- . * + ? ^ $ { } ( ) | [ ] / \

Facciamo un esempio in cui, abbiamo una stringa data come "[controlla questo materiale] è $ 900". Se vuoi prendere l'intera stringa devi dichiararla in questo modo - "\ [controlla \ -questa \ -stuff \] è \ $ 900". Il sistema accetta solo questo modello. Non usiamo i modelli di caratteri escakpe in MooTools. Abbiamo il metodo escapeRegExp () per ignorare i caratteri di escape. Dai un'occhiata al seguente codice.

Esempio

<!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_1 = function(){
            var test_string = $('regex_value').get('value');
            var regex_value = $('regex_match_1').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");
            }
         }
         
         var regex_demo_2 = function(){
            var test_string = $('regex_value').get('value');
            var regex_value = $('regex_match_1').get('value');
            regex_value = regex_value.escapeRegExp();
            var test_result = test_string.test(regex_value);
            
            if (test_result){
               $('regex_2_result').set('html', "Matched");
            } else {
               $('regex_2_result').set('html', "Not Match");
            }
         }
         
         window.addEvent('domready', function() {
            $('regex_1').addEvent('click', regex_demo_1);
            $('regex_2').addEvent('click', regex_demo_2);
            $('regex_3').addEvent('click', regex_demo_3);
         });
      </script>
   </head>
   
   <body>
      String: <input type = "text" id = "regex_value"/><br/><br/>
      Reg Exp 1: <input type = "text" id = "regex_match_1" size = "6"/><br/><br/>
      <input type = "button" id = "regex_1" value = "With escapeRegExp()"/> 
      <Lable id = "regex_1_result"></Lable><br/><br/>
      <input type = "button" id = "regex_2" value = "Without escapeRegExp()"/> 
      <Lable id = "regex_2_result"></Lable><br/><br/>
   </body>
   
</html>

Riceverai il seguente output:

Produzione