3 Example(s) of JavaScript split function


Description :

JavaScript split will split the string with passed parameters into array. Two parameters a) split character b) no of split. See the code split:


JavaScript split function Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
        
</head>

<body>
 <button onclick="splitString()">Show the split values</button>

<p id="sample"></p>
<script>
function splitString() {
    var str = "I Love LearnKode";
    var result = str.split(" ");
    document.getElementById("sample").innerHTML = result;
}
</script>

</body>
</html>

Output

Description :

Example of JavaScript split function with regex expression:


JavaScript split function Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
var names = 'Visrosoftware ;Ian bell; India ; Chandigarh ;London ';
var regexp = /\s*;\s*/;
var result= names.split(regexp );

document.write(result);
</script>

</body>
</html>

Output

Description :

JavaScript split function with second parameter that specify the number of splits.


JavaScript split function Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
  <script>
    var str = "I love learnkode, Learn the code with examples.";
    var result= str.split(" ", 3);
     document.write(result);
  </script>

</body>
</html>

Output