...

View Full Version : simple javascript problem that i need help please



LJackson
12-31-2010, 04:24 PM
Hi

i am trying to combine these two statments

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

Krupski
12-31-2010, 04:53 PM
Hi

i am trying to combine these two statments

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:



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:



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

Dormilich
12-31-2010, 05:31 PM
also is there a javascript function which does the same as urlencode?
encodeURIComponent()

Krupski
12-31-2010, 06:16 PM
encodeURIComponent()

Thank you. I forgot to answer the OP's second question..... :thumbsup:

Old Pedant
01-01-2011, 06:34 AM
Why not just do it the dead simple way?


searchValue = searchValue.replace( /\[[^\]]*\]/g,"").replace( /\([^\)]*\)/g, "");

And I assume that was a typo in the original post:
replace( /\[[^\)]*\]/g,"").

LJackson
01-01-2011, 10:05 PM
cool thanks all
really appreciate your replys and great help

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

LJackson
01-01-2011, 10:55 PM
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

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

Krupski
01-02-2011, 02:36 AM
Why not just do it the dead simple way?


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?



/* 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!!! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum