2016年3月2日水曜日

Javascript 特殊文字のエンコード

Javascript 特殊文字のエンコード


function fn_esc(q){
      var x = {'&':'%26',
                 '"':'%22',
                 };
      for(var i in x){
           q= q.replace(RegExp(i,'g'),x[i]);
           }
         return q;
   };

console.log(fn_esc('&&&""""'));
%26%26%26%22%22%22%22

"樂凡%26祁隆 - 愛%22你一%22生"




修正
2016/04/09 8:18:35

function fn_replace(q){
        var x = {'\&'        :        '%26', 
                 '\"'        :        '%22',
                 '\''        :        '%27%27',
                 ' '        :         '%20',
                 '\n'        :        '%0d%0a'
                 };
        for(var i in x){
                q= q.replace(RegExp(i,'g'),x[i]);
        }
        return q;
}








0 件のコメント: