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

Discussion in 'Javascript' started by romayankin@gmail.com, Jul 16, 2006.

  1. Guest

    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"
    , Jul 16, 2006
    #1
    1. Advertising

  2. wrote in news: 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/
    Rob Williscroft, Jul 16, 2006
    #2
    1. Advertising

  3. Re: Striping spaces from the beginning and end of a string throughRegExp

    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
    Douglas Crockford, Jul 17, 2006
    #3
  4. Randy Webb Guest

    Re: Striping spaces from the beginning and end of a string throughRegExp

    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/
    Randy Webb, Jul 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walterbyrd
    Replies:
    5
    Views:
    1,938
    Paul McGuire
    May 17, 2007
  2. Kevin Blount
    Replies:
    2
    Views:
    118
    Kevin Blount
    Dec 6, 2005
  3. Joao Silva
    Replies:
    16
    Views:
    340
    7stud --
    Aug 21, 2009
  4. Jesse B.
    Replies:
    9
    Views:
    219
    Jesse B.
    Mar 27, 2010
  5. johngnub
    Replies:
    1
    Views:
    90
    J. Gleixner
    Apr 10, 2007
Loading...

Share This Page