日期:2014-05-16  浏览次数:20538 次

js怎么截取包含空格的字符串呀.
js怎么截取包含空格的字符串呀.谢谢.

------解决方案--------------------
<script type= "text/javascript ">
function Left(s, n) {
if (s.length <= n) return s;
else {
var a = new Array, p = /([\S\s])/g;
while (p.exec(s) && n > 1) {
if (RegExp.$1 != " ") n --;
else if (n ==1) break;
a[a.length] = RegExp.$1;
}
return a.join( " ")
}
}
var wc = "hello world ";
alert([ "| " + Left(wc, 5) + "| ",
"| " + Left(wc, 6) + "| ",
"| " + Left(wc, 7) + "| ",
"| " + Left(wc, 20) + "| "].join( "\n "));
</script>