trim string in javascript

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

    trim string in javascript

    for all your javascript trimming needs...

    function trim(inputStrin g) {
    if (typeof inputString != "string") return inputString;
    return inputString
    //clear leading spaces and empty lines
    .replace(/^(\s|\n|\r)*((. |\n|\r)*?)(\s|\ n|\r)*$/g,"$2")

    //take consecutive spaces down to one
    .replace(/(\s(?!(\n|\r))( ?=\s))+/g,"")

    //take consecutive lines breaks down to one
    .replace(/(\n|\r)+/g,"\n\r")

    //remove spacing at the beginning of a line
    .replace(/(\n|\r)\s/g,"$1")

    //remove spacing at the end of a line
    .replace(/\s(\n|\r)/g,"$1");
    }

    -- Jon
  • Michael Winter

    #2
    Re: trim string in javascript

    On 6 May 2004 07:48:53 -0700, Jon Kelling <skater0i@mac.c om> wrote:

    [snip]
    [color=blue]
    > //clear leading spaces and empty lines
    > .replace(/^(\s|\n|\r)*((. |\n|\r)*?)(\s|\ n|\r)*$/g,"$2")[/color]

    Clears leading spaces, but not empty lines.
    [color=blue]
    > //take consecutive spaces down to one
    > .replace(/(\s(?!(\n|\r))( ?=\s))+/g,"")[/color]

    That does work, but will only do so in browsers that support look-ahead
    assertions.
    [color=blue]
    > //take consecutive lines breaks down to one
    > .replace(/(\n|\r)+/g,"\n\r")[/color]

    That works.
    [color=blue]
    > //remove spacing at the beginning of a line
    > .replace(/(\n|\r)\s/g,"$1")
    >
    > //remove spacing at the end of a line
    > .replace(/\s(\n|\r)/g,"$1");[/color]

    These two don't work.

    It's clear that you haven't read the group FAQ. I suggest you do.

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

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Dominique

      #3
      Re: trim string in javascript

      String.prototyp e.trim = function() {
      return this.replace(/(^\s*)|(\s*$)/g, "");
      }


      and you call it like so:

      strVal = strVal.trim()


      cheers!


      "Jon Kelling" <skater0i@mac.c om> wrote in message
      news:a9cb09d3.0 405060648.4d594 bd1@posting.goo gle.com...[color=blue]
      > for all your javascript trimming needs...
      >
      > function trim(inputStrin g) {
      > if (typeof inputString != "string") return inputString;
      > return inputString
      > //clear leading spaces and empty lines
      > .replace(/^(\s|\n|\r)*((. |\n|\r)*?)(\s|\ n|\r)*$/g,"$2")
      >
      > //take consecutive spaces down to one
      > .replace(/(\s(?!(\n|\r))( ?=\s))+/g,"")
      >
      > //take consecutive lines breaks down to one
      > .replace(/(\n|\r)+/g,"\n\r")
      >
      > //remove spacing at the beginning of a line
      > .replace(/(\n|\r)\s/g,"$1")
      >
      > //remove spacing at the end of a line
      > .replace(/\s(\n|\r)/g,"$1");
      > }
      >
      > -- Jon[/color]


      Comment

      • Jon Kelling

        #4
        Re: trim string in javascript

        I wrote my patterns quick under the idea that ^ and $ may match the end
        of any line as opposed to the entire string input in some cases. Without
        the time at work to dedicate to testing in all cases, I felt it safe to
        use (.|\n|\r) in the middle to account for that (rendering the last
        group fairly useless, but oh well). The rest of the patterns i wrote
        quickly to cleanup any unnecessary data that I don't want to be storing,
        worked for me, and thought I would include them on here. If the above
        scenario is not a problem for all multiline input strings given
        different browsers and especially platforms, it would be cool to have
        confirmation on that, otherwise, it works fine for me.

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        Working...