开发者

trying to replace all \n inside textarea to '\n (4 spaces) ' and viceversa but only one \n is found

here's the link where I'm trying this:

http://jsbin.com/ajirim/2/edit#source

the js code:

$(function(){
    $('button').click(dao);
    });

   function dao()            {
   var x = $('textarea').val();

   if(x.substring(0,4) == '    ')
   {
      x = x.replace('\n   开发者_如何学JAVA ','\n').substr(4);
   }
   else
   {
      x = '    '+ x.replace('\n', '\n    ');
   }

   $('textarea').val(x);
}


You're missing the g flag:

   if(x.substring(0,4) == '    ')
   {
      x = x.replace(/\n {4}/g,'\n').substr(4);
   }
   else
   {
      x = '    '+ x.replace(/\n/g, '\n    ');
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜