Javascript 如何删除数组中的空格?
我试图创建一个程序,将单词存储在一个数组中,我所做的是,无论程序找到一个分隔符,或者将它推入数组,我这里的问题是,它甚至用它存储分隔符,我必须使用数组分隔符Javascript 如何删除数组中的空格?,javascript,arrays,Javascript,Arrays,我试图创建一个程序,将单词存储在一个数组中,我所做的是,无论程序找到一个分隔符,或者将它推入数组,我这里的问题是,它甚至用它存储分隔符,我必须使用数组分隔符 var sentence = prompt(""); var tab = []; var word = "" ; var separators = [" ", ","]; for(var i = 0 ; i< sentence.length ; i++){ for(var j = 0 ; j < separat
var sentence = prompt("");
var tab = [];
var word = "" ;
var separators = [" ", ","];
for(var i = 0 ; i< sentence.length ; i++){
for(var j = 0 ; j < separators.length ; j++){
if(sentence.charAt(i) != separators[j] && j == separators.length-1){
word += sentence.charAt(i);
}else if(sentence.charAt(i) == separators[j]){
tab.push(word);
word = "";
}
}
}
tab.push(word);
console.log(tab);
我只会使用正则表达式:
var words = sentence.split(/[, ]+/);
如果要修复代码,请使用indexOf而不是for循环:
我只会使用正则表达式:
var words = sentence.split(/[, ]+/);
如果要修复代码,请使用indexOf而不是for循环:
您可以尝试以下方法:
var text = 'Some test sentence, and a long sentence';
var words = text.split(/,|\s/);
var text = 'Some test sentence, and a long sentence',
s = [',', ' '],
r = RegExp('[' + s.join('') + ']+'),
words = text.split(r);
如果不需要空字符串:
var words = text.split(/,|\s/).filter(function (e) {
return e.length;
});
console.log(words); //["some", "test", "sentence", "and", "a", "long", "sentence"]
如果需要使用阵列,可以尝试以下方法:
var text = 'Some test sentence, and a long sentence';
var words = text.split(/,|\s/);
var text = 'Some test sentence, and a long sentence',
s = [',', ' '],
r = RegExp('[' + s.join('') + ']+'),
words = text.split(r);
您可以尝试以下方法:
var text = 'Some test sentence, and a long sentence';
var words = text.split(/,|\s/);
var text = 'Some test sentence, and a long sentence',
s = [',', ' '],
r = RegExp('[' + s.join('') + ']+'),
words = text.split(r);
如果不需要空字符串:
var words = text.split(/,|\s/).filter(function (e) {
return e.length;
});
console.log(words); //["some", "test", "sentence", "and", "a", "long", "sentence"]
如果需要使用阵列,可以尝试以下方法:
var text = 'Some test sentence, and a long sentence';
var words = text.split(/,|\s/);
var text = 'Some test sentence, and a long sentence',
s = [',', ' '],
r = RegExp('[' + s.join('') + ']+'),
words = text.split(r);
在重新检查该问题后,我认为您需要将本机字符串函数和删除数组中“falsy”项的组合:
$('#textfield).keyup(analyzeString);
var words;
function analyzeString(event){
words = [];
var string = $('#textfield).val()
//replace commas with spaces
string = string.split(',').join(' ');
//split the string on spaces
words = string.split(' ');
//remove the empty blocks using underscore compact
_.compact(words);
}
在重新检查该问题后,我认为您需要将本机字符串函数和删除数组中“falsy”项的组合:
$('#textfield).keyup(analyzeString);
var words;
function analyzeString(event){
words = [];
var string = $('#textfield).val()
//replace commas with spaces
string = string.split(',').join(' ');
//split the string on spaces
words = string.split(' ');
//remove the empty blocks using underscore compact
_.compact(words);
}
.filter不是必需的。只需确保您的正则表达式是贪婪的:[,\s]+。是的,它与split方法一起工作,但我必须使用数组分隔符=[,];试试这个:var r=newregexp'['+s.join+']';.filter不是必需的。只需确保您的正则表达式是贪婪的:[,\s]+。是的,它与split方法一起工作,但我必须使用数组分隔符=[,];试试这个:var r=newregexp'['+s.join+']';你能解释一下这是如何将句子分解成单词的吗?你可以用每个键分析整个字符串。事实上,这可能是一个更好的方法。我会为你重写我的答案。你能解释一下这是如何将句子分解成单词的吗?你可以用每个键分析整个字符串。事实上,这可能是一个更好的方法。我会为你改写我的答案。