[英]Output strings to arrays and output items from array
I'm sure this is really simple but I am learning Javascript and I can't figure this out. 我敢肯定这确实很简单,但是我正在学习Javascript,无法弄清楚。
var niceDay = "please, have a nice day";
匹配非空格:
niceday.match(/\S+/g);
You can use the 'split' javascript function. 您可以使用“拆分” javascript函数。 This will split a string on a certain character and return an array: 这将在特定字符上分割字符串并返回一个数组:
var array = niceDay.split(' ');
This will return an array split on each space in the string. 这将在字符串的每个空格上返回一个数组拆分。 You can then access the second item in the array using: 然后,您可以使用以下命令访问数组中的第二项:
var item = array[1];
Or assign the second item a new value using: 或使用以下方法为第二项分配新值:
array[1] = 'string';
Well this should be simple, that's true 好吧,这应该很简单,这是真的
Here's a solution 这是一个解决方案
var niceDay = "please, have a nice day";
var niceDarray = niceDay.split(' '); // splits the string on spaces
// alerts each item of the array
for (var i = 0; i < niceDarray.length; i++) {
alert(niceDarray[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.