Use RegExp in string split function - Javascript String Operation

Javascript examples for String Operation:String Split

Description

Use RegExp in string split function

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <p id="answer"></p> 
      <script>
        var foo = 'property:value  property2:"value2"  property3>"value3"  -property4:"value4"';
        arr =  foo.split(/[-:<>\s]+/);
        str = "";
        for (i = 0; i < arr.length; i+=2) {
            str = str + arr[i] + " : " + arr[i+1] + " <br>";
        }//from  w ww. ja  v a  2  s .co m
        document.getElementById("answer").innerHTML = str;
    
      </script>  
   </body>
</html>

Related Tutorials