替换1.Is d is the cost of a of gasoline going up up?
要求匹配间隔一个之后的字符是否相同,如果相同就留第一个
要求匹配间隔一个之后的字符是否相同,如果相同就留第二个举一反三
要求匹配间隔2个之后的字符是否相同,如果相同就留第二个
要求匹配间隔2的倍数个之后的字符是否相同,如果相同就留第二个JavaScript正则

解决方案 »

  1.   


    var str="Is d is the cost of a of gasoline going up up";
    str.replace(/\b(\w+)(\s.*?)(\1)/ig,'$2$1');
      

  2.   

    你的结果 d Is the cost a of gasoline going up
    原始字符 Is d is the cost of a of gasoline going up up 
    虽然有差别
    谢谢你提供思路,也请教下呢,找出第4个空格,怎么弄哦
      

  3.   


    //第4个空格替换成 ","
    "Is d is the cost of a of gasoline going up up".replace(/(((^|\s+)\S+){4})(\s)/,"$1,")