如何替换除第一个之外的所有单个字符

时间:2013-03-27 20:19:07

标签: javascript jquery ajax url

我需要替换除第一个之外的所有单个字符。 因为我使用我用Python编程的服务器。 我在Url上传递的信息。 网址是:

http://localhost:8100/index2?decode=demo_title=Demo+title&proc1_script=script.sh+parameters&inputp3_id=pepepe&outputp2_value=&demo_input_description=hola+mundo&outputp4_visible=on&inputdata1_max_pixels=1024000&inputdata1_image_depth=1x8i&tag=&outputp1_id=nanana&proc1_src_compresion=zip&outputp3_description=&inputp1_type=text&inputdata1_description=input+data+description&inputp2_description=bien%3F&inputp3_description=funciona&outputp3_type=header&outputp2_visible=on&outputp3_visible=on&outputp1_type=header&outputp3_value=&demo_params_description=va+bien&demo_result_description=win%3F&outputp1_description=&inputdata1_type=image2d&inputp1_value=si&proc1_chk_script=off&outputp4_info=&outputp2_id=nanfdsvfa&inputp1_description=funciona&demo_wait_description=boh&outputp4_description=&inputp2_id=papapa&inputdata1_id=id_lsd&inputp1_visible=on&proc1_chk_cmake=off&proc1_cmake=-D+CMAKE_BUILD_TYPE%3Astring%3DRelease+&inputp3_visible=+off&outputp1_visible=on&outputp1_value=&outputp4_value=&inputp2_visible=on&proc1_source=lsd-1.5.zip&inputp3_value=si&proc1_make=-j4+-C+&images_config_file=cfgmydemo.cfg&outputp2_type=header&proc1_subdir=xxx-1.5&proc1_chk_make=on&inputp1_id=popopo&proc1_url=http%3A%2F%2Fwww.ipol.im%2Fpub%2Falgo%2F...&inputp2_value=no&demo_data_filename=data_saved.cfg&inputdata1_info=info_lsd&outputp3_info=&inputdata1_image_format=.pgm&inputdata1_compress=False&outputp1_info=&outputp3_id=nananartrtrt&inputp2_type=integer&proc1_id=lsd&inputp3_type=float&outputp2_description=&outputp4_type=header&outputp2_info=&outputp4_id=nana&tag=&inputp4_id=popopo&inputp4_type=text&inputp4_description=cddc&inputp4_value=sdlk&inputp4_info=dsl

现在我遇到这个问题,第一个字符在URL上很重要,而不必替换。

这是我用于替换Javascript中所有字符的脚本:

var $fm = $("#formparam");
                $.get($fm.attr('${nextstep}'))
                    .done(function(data, ok){
                    var fr=$fm.serialize();
                    fr=fr.replace(/\&/g,"+%23+");
                    fr=fr.replace(/\=/g,"%24+");
                    alert('call done: ' +fr);
                    // data is the content of the response
                    document.location.href+="index2?"+fr})
                    .fail(function(data){
                    alert('call failed');
                    // call failed for some reason -- add error messaging?
                    });

如何更改脚本而不替换第一个=?

更新

我想要更改所有单个caracters =而不是第一个。

实施例

some=string=here=more_people 

some=string_here_more_people 

2 个答案:

答案 0 :(得分:3)

我认为这应该可以解决问题:

function replacew(str,pattern,value){
    int index=str.search(pattern);
    return str.substr(0,index+1)+str.substr(index+1).replace(pattern,value);
}

答案 1 :(得分:2)

http://jsfiddle.net/EarUc/5/

从一些字符串开始(empieza con alguna string

var str1 = "=moo=kie=";

调用函数:( llame la funcion)

alert(replaceButFirst(str1, "="));

包含以下代码:(incluya el codigo siguiente para usarlo)

function replaceButFirst(strOriginal, strToReplace){

var n=strOriginal.indexOf(strToReplace);
var firstPart = strOriginal.substring(0, n + 1);
    var tempString = strOriginal.substring(n + 1);
tempString = tempString.replaceAll(strToReplace, "");

return firstPart + tempString;

}

可以找到该功能的原始用途here

String.prototype.replaceAll = function(str1, str2, ignore) 
{
    return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}