There is an elegant way to split a string into an array based on the example:
var x = "FAT* FAT32*";
And that would result in something like this:
x[0] = "FAT*";
x[1] = "FAT32*";
There is an elegant way to split a string into an array based on the example:
var x = "FAT* FAT32*";
And that would result in something like this:
x[0] = "FAT*";
x[1] = "FAT32*";
Yes, there is, using split
.
var x = "FAT* FAT32*";
var array = x.split(" ");
meuLog(array[0]);
meuLog(array[1]);
function meuLog(msg) {
div = document.body;
div.innerHTML = div.innerHTML + '<p>' + msg + '</p>';
}
You can use the method split
as already mentioned, together with the expression \s+
, which will correspond to one or more spaces.
var str = "FAT* FAT32*";
var match = str.split(/\s+/);
alert(match[0]);
alert(match[1]);
An alternative is to get these values through the regular expression /([\w\*]+)/g
, which will correspond to alphanumeric characters and an asterisk.
var str = "FAT* FAT32*";
var match = str.match(/([\w\*]+)/g);
alert(match[0]);
alert(match[1]);