newbie question - regular expression

Discussion in 'Javascript' started by rithish@gmail.com, Mar 14, 2006.

  1. Guest

    I am trying to implement a String trim function as told in
    http://jibbering.com/faq/#FAQ4_16
    I must be doing something wrong as I am unable to trim a given string.
    Below is the test code. I am on IE6. Can someone point out to where I
    am going wrong? Your help is appreciated.

    [...snippet...]
    <HTML>
    <HEAD>
    <SCRIPT TYPE="TEXT/JAVASCRIPT">
    if ( typeof String.prototype.trim == "undefined" ) {
    String.prototype.trim = new Function("return
    this.replace(/^\\s+|\\s+$/g,'')") }

    function TestStringTrim ()
    {
    var trim_str = ' string ';
    trim_str.trim();
    alert ( 'after executing trim : ' + trim_str.length );
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM ID='frmTestJs' NAME='frmTestJs'>
    <INPUT TYPE='BUTTON' ID='btnTest' NAME='btnTest' VALUE='Test Trim'
    onClick='javascript:TestStringTrim();'>
    </FORM>
    </BODY>
    </HTML>
    [.../snippet...]

    Regards,
    Rithish.
    , Mar 14, 2006
    #1
    1. Advertising

  2. VK Guest

    wrote:
    > I am trying to implement a String trim function as told in
    > http://jibbering.com/faq/#FAQ4_16
    > I must be doing something wrong as I am unable to trim a given string.
    > Below is the test code. I am on IE6. Can someone point out to where I
    > am going wrong? Your help is appreciated.
    >
    > [...snippet...]
    > <HTML>
    > <HEAD>
    > <SCRIPT TYPE="TEXT/JAVASCRIPT">
    > if ( typeof String.prototype.trim == "undefined" ) {
    > String.prototype.trim = new Function("return
    > this.replace(/^\\s+|\\s+$/g,'')") }
    >
    > function TestStringTrim ()
    > {
    > var trim_str = ' string ';
    > trim_str.trim();
    > alert ( 'after executing trim : ' + trim_str.length );
    > }
    > </SCRIPT>
    > </HEAD>
    > <BODY>
    > <FORM ID='frmTestJs' NAME='frmTestJs'>
    > <INPUT TYPE='BUTTON' ID='btnTest' NAME='btnTest' VALUE='Test Trim'
    > onClick='javascript:TestStringTrim();'>
    > </FORM>
    > </BODY>
    > </HTML>
    > [.../snippet...]


    The problem is that the trim() method doesn't change the original
    string: it creates new trimmed string and returns it. Try:
    alert ( 'after executing trim : ' + trim_str.trim().length );
    VK, Mar 14, 2006
    #2
    1. Advertising

  3. Guest

    VK wrote:

    > The problem is that the trim() method doesn't change the original
    > string: it creates new trimmed string and returns it. Try:
    > alert ( 'after executing trim : ' + trim_str.trim().length );


    So.. So.. Dumb of me... That's right VK... :eek:)

    However, is there a way to change the original string?

    Regards,
    Rithish.
    , Mar 15, 2006
    #3
  4. Evertjan. Guest

    wrote on 15 mrt 2006 in comp.lang.javascript:

    > However, is there a way to change the original string?
    >


    theOriginalString = Change(theOriginalString)

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Mar 15, 2006
    #4
  5. Guest

    > theOriginalString = Change(theOriginalString)
    >

    Was that a dig at my ribs Evertjan? :eek:)

    Does this mean a 'no' then?

    Regards,
    Rithish.
    , Mar 15, 2006
    #5
  6. wrote:

    > VK wrote:
    >> The problem is that the trim() method doesn't change the original
    >> string: it creates new trimmed string and returns it. Try:
    >> alert ( 'after executing trim : ' + trim_str.trim().length );

    >
    > So.. So.. Dumb of me... That's right VK... :eek:)
    >
    > However, is there a way to change the original string?


    No, string values are immutable. You can only create a new string
    value from them.


    PointedEars
    Thomas 'PointedEars' Lahn, Mar 15, 2006
    #6
  7. Evertjan. Guest

    wrote on 15 mrt 2006 in comp.lang.javascript:

    >> theOriginalString = Change(theOriginalString)
    >>

    > Was that a dig at my ribs Evertjan? :eek:)
    >
    > Does this mean a 'no' then?


    It was an answer to your question you skipped in quoting:

    >> However, is there a way to change the original string?


    So: yes, there is.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Mar 15, 2006
    #7
    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. VSK
    Replies:
    2
    Views:
    2,268
  2. Lee
    Replies:
    6
    Views:
    347
    Alan Moore
    Oct 14, 2003
  3. googleboy
    Replies:
    6
    Views:
    320
    Paul McGuire
    Sep 23, 2005
  4. Peter Vanderhaden
    Replies:
    5
    Views:
    123
    SpringFlowers AutumnMoon
    Sep 30, 2007
  5. Sven
    Replies:
    5
    Views:
    79
    Helgi Briem
    Oct 24, 2003
Loading...

Share This Page