Javascript Data Type How to - Split string into a unknown number of strings by comma








Question

We would like to know how to split string into a unknown number of strings by comma.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!-- w w w.ja v a2 s  . com-->
<body>
  <script>
var ids = '124523, 23444, 23, 123, 5345, 534, 867';
var idArray = ids.split(/,\s?/);
idArray.forEach(function(id) {
  document.writeln(id);
  document.writeln('<br/>');
});
</script>
</body>
</html>

The code above is rendered as follows: