开发者

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

开发者 https://www.devze.com 2023-04-06 13:33 出处:网络
here\'s the link where I\'m trying this: http://jsbin.com/ajirim/2/edit#source the js code: $(function(){

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

精彩评论

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