Does Trim works in JavaScript ?

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

    Does Trim works in JavaScript ?

    Hi,

    i used Trim(my_val) and its giving error in JavaScript. is it a correct
    way to trim. Please suggest me, thanks in advance.


  • Randy Webb

    #2
    Re: Does Trim works in JavaScript ?

    mhk wrote:
    [color=blue]
    > Hi,
    >
    > i used Trim(my_val) and its giving error in JavaScript. is it a correct
    > way to trim. Please suggest me, thanks in advance.
    >
    >[/color]

    Read the Group FAQ, it has snippets of code for emulating Trim as
    Javascript has no native Trim()

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • mhk

      #3
      Re: Does Trim works in JavaScript ?



      Randy Webb wrote:
      [color=blue]
      > mhk wrote:
      >[color=green]
      >> Hi,
      >>
      >> i used Trim(my_val) and its giving error in JavaScript. is it a
      >> correct way to trim. Please suggest me, thanks in advance.
      >>
      >>[/color]
      >
      > Read the Group FAQ, it has snippets of code for emulating Trim as
      > Javascript has no native Trim()
      >[/color]

      *************** *************** *************
      the code in your FAQ doesnt handle enter keys. this one is much better.
      found on internet.

      // Trim Function
      // --------------
      String.prototyp e.trim = function()
      {
      // skip leading and trailing whitespace
      // and return everything in between
      var x=this;
      x=x.replace(/^\s*(.*)/, "$1");
      x=x.replace(/(.*?)\s*$/, "$1");
      return x;
      }


      Comment

      • Evertjan.

        #4
        Re: Does Trim works in JavaScript ?

        mhk wrote on 16 jan 2005 in comp.lang.javas cript:
        [color=blue][color=green]
        >> Read the Group FAQ, it has snippets of code for emulating Trim as
        >> Javascript has no native Trim()
        >>[/color]
        >
        > *************** *************** *************
        > the code in your FAQ doesnt handle enter keys.[/color]

        Whose FAQ?

        Keys are not part of a string, so what do you mean|?
        [color=blue]
        > this one is much better.
        > found on internet.
        > // Trim Function
        > // --------------
        > String.prototyp e.trim = function()
        > {
        > // skip leading and trailing whitespace
        > // and return everything in between
        > var x=this;
        > x=x.replace(/^\s*(.*)/, "$1");
        > x=x.replace(/(.*?)\s*$/, "$1");
        > return x;
        >}
        >[/color]

        a bit overdone regex, methinks.

        x = x.replace(/^\s*/, "");
        x = x.replace(/\s*$/, "");

        or

        x = x.replace(/^\s*(.*?)\s*$/, "$1");

        Problem with your and this last one is,
        that older js may not know lookahead "?"


        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        Working...