Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Striping spaces from the beginning and end of a string through RegExp

Reply
Thread Tools

Striping spaces from the beginning and end of a string through RegExp

 
 
romayankin@gmail.com
Guest
Posts: n/a
 
      07-16-2006
I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:

var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);

Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-16-2006
wrote in news:(E-Mail Removed) ups.com in
comp.lang.javascript:

> I'm trying to create PHP trim() function equivalent using pure JS code
> that truncates spaces from the begging and from the end of the given
> string. One of the solutions is to use two "while" loops that will run
> through the chars from the beginning and then on the end and cut
> spaces. Instead I want to write a RegExp to accomplish this. Here is
> the sample to give you an idea of my problem:
>
> var str = new String(" and; n4m ")
> var patternTrim = /(^\s+)(.*)(\s+$)/;
> var arr = str.match(patternTrim);
>
> Note the space char in the middle of the evaluated string. The
> resulting string i want to have looks like this "and; n4m"
>
>


The trick is to match waht you want to remove (not the whole
string) and then use .replace():

function trim( s )
{
return s.replace( /^\s+|\s+$/g , "" );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      07-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm trying to create PHP trim() function equivalent using pure JS code
> that truncates spaces from the begging and from the end of the given
> string. One of the solutions is to use two "while" loops that will run
> through the chars from the beginning and then on the end and cut
> spaces. Instead I want to write a RegExp to accomplish this. Here is
> the sample to give you an idea of my problem:
>
> var str = new String(" and; n4m ")
> var patternTrim = /(^\s+)(.*)(\s+$)/;
> var arr = str.match(patternTrim);
>
> Note the space char in the middle of the evaluated string. The
> resulting string i want to have looks like this "and; n4m"


First, do not use new String in JavaScript. It doesn't do what you expect. Use
use string literals.

String.prototype.trim = function () {
return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
});

var str = " and; n4m ";
var arr = str.trim();

http://javascript.crockford.com/remedial.html
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      07-17-2006
(E-Mail Removed) said the following on 7/16/2006 6:30 PM:
> I'm trying to create PHP trim() function equivalent using pure JS code
> that truncates spaces from the begging and from the end of the given
> string.


<URL: http://jibbering.com/faq/#FAQ4_16>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
how do I count spaces at the beginning of a string? walterbyrd Python 5 05-17-2007 12:43 PM
Collecting details from router, striping info, map and regex. Example. johngnub Perl Misc 1 04-10-2007 08:45 PM
striping HTML tags from string, but leaving paragraph formatting Kevin Blount ASP General 2 12-06-2005 09:49 PM



Advertisments