Example of how to use the string split function in JavaScript: javascriptsplit
This document describes the use of the string splitting function in JavaScript. Share it with you for your reference. The details are as follows:
Let's take a look at the following code:
<script type="text/javascript">var str="How are you doing today?"document.write(str.split(" ") + "<br />")document.write(str.split("") + "<br />")document.write(str.split(" ",3))</script>
The output result is as follows:
How,are,you,doing,today?H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?How,are,you
Example:
"2: 3: 4: 5 ". split (":") // Returns ["2", "3", "4", "5"] "| a | B | c ". split ("|") // Returns ["", "a", "B", "c"]
Use the following code to split sentences into words:
var words = sentence.split(' ')
If you want to split words into letters or strings into characters, use the following code:
"Hello". split ("") // you can return ["h", "e", "l", "l", "o"]
If you only need to return a part of the characters, use the howmany parameter:
"Hello". split ("", 3) // you can return ["h", "e", "l"]
Or use the regular expression as the separator:
var words = sentence.split(/\s+/)
I hope this article will help you design javascript programs.