JavaScript Tutorial/String/split

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Regular Expression Only

<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(myRegExp), "Regular Expression Only");
    document.close();
    -->
    </script>
</html>


Regular Expression With Limit of 3

<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(myRegExp, 3), "Regular Expression With Limit of 3");
    document.close();
    -->
    </script>
</html>


Separator With Limit of 2

<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, 2), "Separator With Limit of 2");
    document.close();
    -->
    </script>
</html>


String.split()

Syntax



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