Trimming a String Using Regular Expressions : String « Language Basics « JavaScript DHTML






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>

           
         
    
  








Related examples in the same category

1.Demo all String methods
2.String utility: word count, replace and capitalize
3.Strip Commas
4.Text Range Search and Replace (IE only)
5.Counting the Words in a Text String
6.Reversing a String
7.String encode and decode
8.Capitalizing the First Letter in Each Word of a String
9.Playing with Strings
10. Using the String Object's Link Method
11.Using a for Loop to Reverse a String
12. Concatenate JavaScript String
13.String length: number of characters in a string.
14.String fontcolor(): a string in a specified color
15.String indexOf(): string position
16.String Validation
17.Using Quotes within Strings
18.Using the String Object
19.String toUpperCase
20.Lab for string.replace() and string.search()
21.Slicing a String
22.A String Object Prototype
23.Creating a Custom toString() Method
24.Reading a Portion of a String
25.Source Code for a Sample Page That Formats a String Object with the 'a' Tag
26.Source Code for Our String-Formatting Script
27.Adding a replace() Method to the String Object
28.Creating a Function That Will Search and Replace in Strings
29. Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
30.Methods and Properties of the String Object
31.String match(): returns the text if found
32.String substr() and substring(): returns a specified part of a string
33.String toLowerCase() and toUpperCase(): converts a string to lowercase and uppercase
34.Converting Strings to Upper Case
35.String encoder
36.Concatenate two string variables together
37.Search string value in an array