简洁的强制换行代码

经典的长字符串非 ie 下强制换行问题 ,网上大多代码看起来比较啰嗦,自己重写个把,原理很简单,长英文串超过一定字符就加个 <wbr/> 建议换行标签就好了 。

 

 

代码 :

 

欢迎挑错

 

/**
*每count长字串间加入<wbr/>建议换行标志
*@param str {string} 长字串
*@param count {int} 
*@return {string} 加好建议换行标志的字符串
*/
function breadWord(str,count) {
	//超过count需要换行的英文符号,不包括, .等标点
	var matchRe=new RegExp("[a-zA-Z!@#$%^&*_+=\\-]{"+count+",}","g");
	var sRe=new RegExp(".{"+count+"}|(?:.{1,"+count+"}$)","g");
	return str.replace(matchRe,function(m){
		return m.match(sRe).join("<wbr/>");
	})
}

alert(breadWord("[email protected]",5))

你可能感兴趣的:(IE)