JS的split函数用法
- <script language="javascript">
- function spli(){
- datastr="2,2,3,5,6,6";
- var str= new Array();
- str=datastr.split(",");
- for (i=0;i<str.length ;i++ )
- {
- document.write(str[i]+"<br/>");
- }
- }
- </script>
若要实现有多个分隔符分隔的字符串,具体代码如下:
- <html>
- <head>
- <script language="javascript" type="text/javascript">
- function window_load(){
- var str = "name,age,sex;address";
- var reg = /[,;]/; //用正则表达式,指定分隔符为 逗号或者分号.
- alert(str.split(reg).join("\n"));
- }
- </script>
- </head>
- <body onload="window_load();">
- </body>
- </html>