String.split() : split « String « JavaScript Tutorial






Syntax

string.split(separator, num)
    string.split(separator)
    string.split(regexpression, num)
    string.split(regexpression)

The split() method splits the string into separate strings based on the regular expression or separator.

The method returns an array containing each of the segments found in the string.

See the reference entry for RegExp.

<html>
    <script language="JavaScript1.2">
    <!--
    function genResults(arrayName, testName){
      document.write('<b>Currently Evaluating: ' + testName + '</b><hr>');
      if(arrayName == null){
        document.write('No matches were found');
      }else{
        for(var i = 0; i < arrayName.length; i++){
          document.write('[' + i + ']: ' + arrayName[i] + '<br>');
        }
      }
      document.write('<P>');
    }

    var myString = new String("This is a test");
    var myRegExp = /\s/g;
    var mySeparator = " ";

    genResults(myString.split(mySeparator), "Separator Only");
    document.close();
    -->
    </script>
</html>








6.27.split
6.27.1.String.split()
6.27.2.Separator With Limit of 2
6.27.3.Regular Expression Only
6.27.4.Regular Expression With Limit of 3