javascript 문자열 캡 처 (substring 을 통 해 중 영문 혼합 지원)

2858 단어
javascript 문자열 캡 처 (영어 혼합 지원)
 
  
<br>var sub=function(str,n){ <br>var r=/[^\x00-\xff]/g; <br>if(str.replace(r,"mm").length<=n){return str;} <br>var m=Math.floor(n/2); <br>for(var i=m;i<str.length;i++){ <br>if(str.substr(0,i).replace(r,"mm").length>=n){ <br>return str.substr(0,i)+"..."; <br>} <br>} <br>return str; <br>} <br>alert(sub(' Javascript (Js )',15)) <br>

js 방법 으로 substring ()
 
  
var str = "0123456789";
alert(str.substring(0));------------"0123456789"
alert(str.substring(5));------------"56789"
alert(str.substring(10));-----------""
alert(str.substring(12));-----------""
alert(str.substring(-5));-----------"0123456789"
alert(str.substring(-10));----------"0123456789"
alert(str.substring(-12));----------"0123456789"
alert(str.substring(0,5));----------"01234"
alert(str.substring(0,10));---------"0123456789"
alert(str.substring(0,12));---------"0123456789"
alert(str.substring(2,0));----------"01"
alert(str.substring(2,2));----------""
alert(str.substring(2,5));----------"234"
alert(str.substring(2,12));---------"23456789"
alert(str.substring(2,-2));---------"01"
alert(str.substring(-1,5));---------"01234"
alert(str.substring(-1,-5));--------""

js 방법 으로 substr ()
 
  
var str = "0123456789";
alert(str.substr(0));---------------"0123456789"
alert(str.substr(5));---------------"56789"
alert(str.substr(10));--------------""
alert(str.substr(12));--------------""
alert(str.substr(-5));--------------"0123456789"
alert(str.substr(-10));-------------"0123456789"
alert(str.substr(-12));-------------"0123456789"
alert(str.substr(0,5));-------------"01234"
alert(str.substr(0,10));------------"0123456789"
alert(str.substr(0,12));------------"0123456789"
alert(str.substr(2,0));-------------""
alert(str.substr(2,2));-------------"23"
alert(str.substr(2,5));-------------"23456"
alert(str.substr(2,12));------------"23456789"
alert(str.substr(2,-2));------------""
alert(str.substr(-1,5));------------"01234"
alert(str.substr(-1,-5));-----------""

좋은 웹페이지 즐겨찾기