Taking out white space in string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ashlie

    Taking out white space in string

    I am trying to develop a script that takes out the white space at the
    FRONT and the BACK of a string but not the middle. I have a string
    that looks like this:

    XXXX XXXX XXXX XXXX

    The groupings within the string can be any length...howeve r, in order
    to dump this data into a SQL database, the potential added white space
    at the front or the back of the string needs to be deleted as the user
    moves from one text box in a form to the next.

    Any ideas?
  • Evertjan.

    #2
    Re: Taking out white space in string

    Ashlie wrote on 08 jul 2003 in comp.lang.javas cript:[color=blue]
    > I am trying to develop a script that takes out the white space at the
    > FRONT and the BACK of a string but not the middle. I have a string
    > that looks like this:
    >
    > XXXX XXXX XXXX XXXX
    >
    > The groupings within the string can be any length...howeve r, in order
    > to dump this data into a SQL database, the potential added white space
    > at the front or the back of the string needs to be deleted as the user
    > moves from one text box in a form to the next.[/color]


    <script>
    function trim(s){
    return s.replace(/^\s*(.*?)\s*$/,"$1")
    }

    alert(">"+trim( ' XXXX XXXX XXXX XXXX ')+"<")
    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Douglas Crockford

      #3
      Re: Taking out white space in string

      > I am trying to develop a script that takes out the white space at the[color=blue]
      > FRONT and the BACK of a string but not the middle. I have a string
      > that looks like this:
      >
      > XXXX XXXX XXXX XXXX
      >
      > The groupings within the string can be any length...howeve r, in order
      > to dump this data into a SQL database, the potential added white space
      > at the front or the back of the string needs to be deleted as the user
      > moves from one text box in a form to the next.[/color]

      Check out the trim method here:


      Comment

      • frogcoder

        #4
        Re: Taking out white space in string

        This works fine for me,

        trim(stringValu e)
        {
        return stringValue.rep lace(/(^\s*|\s*$)/, "");
        }

        Douglas Crockford wrote:[color=blue][color=green]
        >>I am trying to develop a script that takes out the white space at the
        >>FRONT and the BACK of a string but not the middle. I have a string
        >>that looks like this:
        >>
        >>XXXX XXXX XXXX XXXX
        >>
        >>The groupings within the string can be any length...howeve r, in order
        >>to dump this data into a SQL database, the potential added white space
        >>at the front or the back of the string needs to be deleted as the user
        >>moves from one text box in a form to the next.[/color]
        >
        >
        > Check out the trim method here:
        > http://www.crockford.com/javascript/remedial.html
        >[/color]

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Taking out white space in string

          frogcoder <seanliu@ms77.u rl.com.tw> writes:

          Please don't top post.
          [color=blue]
          > This works fine for me,
          >
          > trim(stringValu e)
          > {
          > return stringValue.rep lace(/(^\s*|\s*$)/, "");
          > }[/color]

          Does it now?
          What is the result of
          trim(" foo bar ")
          ?
          It only removes whitespace before *or* after the string, not both.
          It can be fixed simply by adding a "g" after the regexp:
          /(^\s+|\s+$)/g
          (uses + too, there is no reason to replace nothing)

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: Taking out white space in string

            JRS: In article <bhnhk9$90o@net news.hinet.net> , seen in
            news:comp.lang. javascript, frogcoder <seanliu@ms77.u rl.com.tw> posted at
            Sun, 17 Aug 2003 17:24:25 :-[color=blue]
            >This works fine for me,
            >
            >trim(stringVal ue)
            >{
            > return stringValue.rep lace(/(^\s*|\s*$)/, "");
            >}[/color]

            See FAQ sec 4.16 for a correct way of removing both leading & trailing
            white-space.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            Working...