[Javascript] Trim whitespaces using Replace function

Cutter Bl cutterbl at hotmail.com
Mon Jul 9 07:52:16 CDT 2001


Regular expressions always begin and end with a slash,/. The carrot,^, is 
the beginning of a string. any character you want your expression to match 
will be enclosed in brackets, []. the \s is denote a white space character. 
The plus sign + means one or more of whatever the previous item was that 
we're checking on. In this case, the white space. Then you have the ending 
regular expression slash, /. the g is a regular expression modifier to 
search for all possible matches (globally), not just the first one. Within 
the quotation marks are your replace items, in this case it would replace 
white space with no space. In the second version of the script they also are 
using the dollar sign, $, which signifies that the matched string must end 
here. I'm not positive, but this might not be necessary for trimming off 
white space. You might have to ask one of the local gurus on that one.

I pulled this information from "Javascript for the World Wide Web: Fourth 
Edition", Chapter 7. This comes from PeachPit Press. I like the book, as it 
gives a lot of practical example info, explaining scripts step by step.

Cutter
**********************************************
Hi,

      I see the ff expression.

     STRING=STRING.replace(/^[\s]+/g,"");
     STRING=STRING.replace(/[\s]+$/g,"");

      I know that these are expressions to trim the
white spaces but I want to know what is the
meaning of each characters inside the replace
function.

      Thanks in Advance.

Thope

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
http://www.LaTech.edu/mailman/listinfo/javascript

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com





More information about the Javascript mailing list