JavaScript split() method


JavaScript split() Method

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮显示分割后的数组.</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str="How are you doing today?";
	var n=str.split(" ");
	document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Run Instance»

Click the "Run Example" button to view the online example


Definition and usage

The split() method is used to split a string into a string array.

Tip: If the empty string ("") is used as a separator, each character in the stringObject will be split.

Note: The split() method does not change the original string.


Browser support

QQ截图20161108165429.png

All major browsers support the split() method


Syntax

string.split(separator,limit)

Parameter value

ParametersDescription
separatorOptional. A string or regular expression to split the string Object from where specified by this parameter.
limit Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.

Return value

TypeDescription
ArrayA string array. The array is created by splitting the string Object into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.

Technical Details

JavaScript Version: 1.1


More instances

Instances

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str="How are you doing today?";
	var n=str.split();
	document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Run instance»

Click "Run" Instance" button to view the online instance


Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str="How are you doing today?";
	var n=str.split("");
	document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Run instance»

Click the "Run Instance" button to view the online instance


##Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str="How are you doing today?";
	var n=str.split(" ",3);
	document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Run instance»Click the "Run instance" button to view the online instance


Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str="How are you doing today?";
	var n=str.split("o");
	document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Run Instance»Click the "Run Instance" button to view the online instance





##