<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS分解字符串_www.jb200.com</title>
<script type="text/javascript">
String.prototype.trim = function(){
// 用
正则表达式将前后空格,用空字符串替代。
return this.replace(/(^/s*)|(/s*$)/g,"");
}
function ttt(){
var str=" ,你好,gggg,999, ";
alert("old["+str+"]");
strstr=str.replace(",",",");
alert("["+str+"]");
strstr=str.trim();
if(str.substring(0,1)==","){
strstr=str.substring(1,str.length);
}
if(str.substring(str.length-1,str.length)==","){
strstr=str.substring(0,str.length-1);
}
alert("["+str+"]");
var sa=str.split(",");
var i;
for(i=0;i<sa.length;i++){
alert("["+i+"] "+sa[i]);
}
}
</script>
</head>
<body>
<input type="button" name="Submit" onClick="ttt();" value="按钮" />
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>split函数</title>
<script type="text/javascript">
var array;
function getString(str){
array = str.split("|");
document.getElementById("userName").value = array[0];
document.getElementById("userAge").value = array[1];
}
</script>
</head>
<body onload="getString('李世民|22')">
<input type="text" id="userName" name="userName" /><br /><br />
<input type="text" id="userAge" name="userAge" />
</body>
</html>