Trimming a String Using Regular Expressions : String : Language Basics JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Language Basics » String »

 

Trimming a String Using Regular Expressions



/*

Learn How to Program Using Any Web Browser
by Harold Davis 

Apress CopyRight 2004

ISBN: 1590591135
*/

<HTML>
<HEAD>
<TITLE>Strim a tring</TITLE>
<SCRIPT
function ltrim(testStr) { 
   if (testStr == ""
      return ""
   else 
      var pattern = /[^s]+.*/; 
      result = testStr.match(pattern)
      return result[0]
   

function rtrim(testStr) { 
   if (testStr == ""
      return ""
   else 
      var pattern = /.*[S]/; 
      result = testStr.match(pattern)
      return result[0]
   


function trim(testStr) { 
   return rtrim(ltrim(testStr))


</SCRIPT>
</HEAD>
<BODY>
<H1> 
Trim a string today! 
</H1>
<FORM name="theForm">
<TABLE>
<tr>
<td colspan=4
Enter string for trimming: 
</td>
</tr>
<tr>
<td colspan = 4>
<INPUT type=text name=testStr size=60>
</td>
</tr>
<tr>
<td colspan=3>
<INPUT type=button name="theButton" value="Trim" 
   onClick="document.theForm.display.value = 
   trim(document.theForm.testStr.value)";>
</td>
<td>
<INPUT type=button name="theButton" value="Clear" 
   onClick='document.theForm.testStr.value=""
document.theForm.display.value=""'>
</td>
</tr>
<tr>
<td colspan=4>
<br>
<hr> 
Here's the trimmed string: 
<br>
</td>
</tr>
<tr>
<td colspan = 4>
<INPUT type=text name=display size=60>
</td>
</tr>
</TABLE>
</FORM>
</BODY>
</HTML>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Language Basics
» String