Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
eddy_declercq
Active Contributor
0 Kudos

As mentioned in my regular expressions tutorial  (parts 1  (https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/express%20you...),[ 2 | https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/express%20you...] and

3

), I’m more than happy that regular expressions – despite being  a relatively old technology – have finally been incorporated into ABAP as of Netweaver  2004s. In the last part of my tutorials I reviewed some of the tools that are  available in order to make and test regular expressions. If you are a seasoned  DIY person, you might want to do it yourself. There is a DIY show –in fact it’s  a commercial in disguise - on our regional TV where the handy man (called  Roger) always says: “What you do yourself is usually done better”. I won’t go  into the issue of whether that’s true or not though.

Pure  client<br>

            Anyway, there are several methods for testing  your regular expressions. For me, as a web developer, it’s a foregone  conclusion that it needs to be via a web application. You can make a mix of  ABAP and HTML(B), but I won’t go that far for this simple application. It can easily  be done via plain JavaScript. All you need is a form:</p>

          <p>The RE itself</p>

          <pre>Regular expression: <input type="text" name="regex" size="75" /><br></pre>

          <pre> </pre>

          <p>The string we want to test</p>

          <pre>String to test: <input type="text" name="teststring" size="100" /><br></pre>

          <pre> </pre>

          <p>The modifiers</p>

          <pre>All ocurences <input type="checkbox" name="global" value="g" /> </pre>

          <pre>Case insensitive <input type="checkbox" name="insensitive" value="i" /><br></pre>

          <p>And some JavaScript to test the RE.</p>

          <p>Test whether we checked some of the  modifiers</p>

          <pre>

     if (document.regextest.global.checked)

         modifier  += "g";

     if (document.regextest.insensitive.checked)

         modifier  += "i";</pre>

          <p>Create an RE object with the constructor</p>

          <pre>  var regex = new RegExp(document.regextest.regex.value,modifier);</pre>

          <p>Execute that RE against the string</p>

          <pre>  var matchRE = regex.exec(document.regextest.teststring.value);</pre>

          <p>As long as we find results</p>

          <pre>

     while ((matchRE =  regex.exec(document.regextest.teststring.value))!= null)

           </pre>

          <p>We add the position and the found match in  the message  </p>

          <pre>  msg += "
at " + matchRE.index +  "
for "+matchRE;</pre>

          <p> </p>

          <p>Putting it all together, it comes down to  this code.</p>

          <pre><html>

   <head>

   <title>RE  tester</title>

   <SCRIPT  LANGUAGE="JavaScript">

             function  isMatch()

     {

     var modifier = "";

     if (document.regextest.global.checked)

         modifier  += "g";

     if (document.regextest.insensitive.checked)

         modifier  += "i";

     regex = new  RegExp(document.regextest.regex.value,modifier);

     var matchRE;

     var msg = "";

     while ((matchRE =  regex.exec(document.regextest.teststring.value))!= null)

        {

          msg  += "
at " + matchRE.index + "
for "+matchRE;

          }

       if (msg != null)

            {

             alert("Yes, it matches" + msg);

            }

      else

         {

            alert("No, it doesn't match");

           }

     }

   </SCRIPT>

   </head>

   <body>

   <form  name="regextest">

             Regular  expression: <input type="text" name="regex"  size="75" /><br>

             String  to test: <input type="text" name="teststring"  size="100" /><br>

             All  ocurences <input type="checkbox" name="global"  value="g" />

             Case  insensitive <input type="checkbox" name="insensitive"  value="i" /><br></pre>

          <pre>             <input  type="button" value="Does it match?"  onclick="isMatch()" />

   </form>

   </body>

   </html></pre>

          <p> </p>

          <p>The result looks like this. </p>

          <p><p>!https://weblogs.sdn.sap.com/weblogs/images/19902/re_js.jpg|height=231|alt=image|width=475|src=https:...!</body>

8 Comments