Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation simple javascript problem that i need help please

    Hi

    i am trying to combine these two statments
    Code:
    searchValue.replace( /\[[^\)]*\]/g,"");
    searchValue.replace( /\([^\)]*\)/g, "");
    is it possible to put these two into one statment?

    also is there a javascript function which does the same as urlencode?

    many thanks
    Luke

  • #2
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by LJackson View Post
    Hi

    i am trying to combine these two statments
    Code:
    searchValue.replace( /\[[^\)]*\]/g,"");
    searchValue.replace( /\([^\)]*\)/g, "");
    is it possible to put these two into one statment?

    also is there a javascript function which does the same as urlencode?

    many thanks
    Luke
    If you want to replace multiple items in Javascript, you can do this:

    Code:
    function doReplace(text)
    {
        var str = text;
        var src = [/(regx4)/ig, /(regx3)/ig, /(regx2)/ig, /(regx1)/ig];
        var dst = ['replacement4','replacement3','replacement2','replacement1'];
        var n = src.length;
        while(n--)
        {
            str = str.replace(src[n], dst[n]);
        }
        return str;
    }
    Note that since this function goes from [last] to [first], it replaces in "reverse" order. So, if your regex's are position dependent, put them in last first, first last.

    Your code:

    Code:
    function doReplace(text)
    {
        var str = text;
        var src = [/\([^\)]*\)/g,/\[[^\)]*\]/g];
        var dst = ['',''];
        var n = src.length;
        while(n--)
        {
            str = str.replace(src[n], dst[n]);
        }
        return str;
    }

    Hope this helps.....

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by LJackson View Post
    also is there a javascript function which does the same as urlencode?
    encodeURIComponent()
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Dormilich View Post
    encodeURIComponent()
    Thank you. I forgot to answer the OP's second question.....
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Why not just do it the dead simple way?
    Code:
    searchValue = searchValue.replace( /\[[^\]]*\]/g,"").replace( /\([^\)]*\)/g, "");
    And I assume that was a typo in the original post:
    replace( /\[[^\)]*\]/g,"").
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    cool thanks all
    really appreciate your replys and great help

    many thanks and happy new year!!!!!!!!

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi again

    ok i tried modifying my script by trying to combine some of my expressions like old pendant suggested, it worked fine when combining the two expressions but i tried adding more to it

    here is my code
    Code:
    case 13:$("#ajax_response").fadeOut("slow");
    $("#keyword").val($("li[class='selected'] a").text());
    searchValue=document.getElementById('keyword').value;searchValue = searchValue.replace(/^\s*|\s*$/g,'').replace(/[:',]/gi,"").replace( /\[[^\]]*\]/g,"").replace( /\([^\])*\)/g, "");
    searchValue = searchValue.toLowerCase();
    window.location="/"+findCdValue(searchValue)+"/"+encodeURIComponent(searchValue);break;}}}
    and it gives me a unterminated regular expression literal in my firebug console,

    any ideas where im going wrong? am stll coming to terms with javascript

    thanks
    Luke

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    Why not just do it the dead simple way?
    Code:
    searchValue = searchValue.replace( /\[[^\]]*\]/g,"").replace( /\([^\)]*\)/g, "");
    And I assume that was a typo in the original post:
    replace( /\[[^\)]*\]/g,"").
    How long would THIS be, done your way?

    Code:
    /* cleanup html */
    function cleanHTML(text)
    {
        var str=text;
        var src=[/<br \/>/g,/&lt;/g,/&gt;/g,/&amp;/g,/&quot;/g,/&#34;/g,/&#39;/g,/&#40;/g,/&#41;/g];
        var dst=["\n","<",">","&","\"","\"","\'","(",")"];
        var n=src.length;
        while(n--)
        {
            str=str.replace(src[n],dst[n]);
        }
        return str;
    }

    LOL!!!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •