How to split a string in JavaScript

{0 Comments}

To split a string in as per desired delimiter you can use split in built function of the JavaScript which will split the desired string and return the result in the form of array.  In the following example there is a paragraph tag with id name of demo which will help to show the result. Then a button is added to call the custom JavaScript function which will split the string. Here in the split function left space to define that over delimiter is space

 HTML |  copy code |? 
01
<p id="demo">result</p>
02
<button onclick="coolFunction()">Try it</button>
03
<script>
04
function coolFunction()
05
{
06
var str="there is some thing strange";
07
var n=str.split(" ");
08
document.getElementById("demo").innerHTML=n;
09
}
10
</script>
11

Leave a Comment

Your email address will not be published.