这是我想要完成的......
○取一个长字符串并将该字符串的每个单词推入一个数组。 - 注意:我在ITSM工具中使用此代码,因此,一旦我能够将此字符串分解为单独的值,我将使用每个值运行查询。我已经在这里回顾了其他一些帖子,这里是我到目前为止所获得的代码。我知道它还没有完成代码,但那就是我在这里的原因......
var text = "This is a very long string that could be split up into different words and different variables";
var box = [];
var start = 0;
var index = 0;
console.log(chopper(text));
function chopper(string){
var length = string.length;
string = string.replace(/ /g,',');
for(i=0; i < length; i++){
index = string.indexOf(',', start);
console.log("Start : " + start);
console.log("Index : " + index);
var word = string.slice(start,index);
string = string.substr(start,length);
console.log("String : " + string);
start = index;
box.push(word);
}
return box;
}
答案 0 :(得分:1)
要从字符串中创建单词列表(其中“单词”是拉丁字母序列),请使用正则表达式/[^a-z]+/i
进行拆分,这意味着“不是字母”。这会删除空格,标点符号和其他非单词内容。
var text = "This is a very long string, that could be split up: into different words and different variables";
words = text.split(/[^a-z]+/i)
alert(words)
答案 1 :(得分:0)
为什么不拆分字符串?
box=text.split(new RegExp("\\s+"));
答案 2 :(得分:0)
尝试找出你正在做的事情非常困难,但这是我的解释。
function getBox(text) {
// lowercase the text so that we can dedupe the arr properly
text = text.toLowerCase();
// split the text into an array along word boundaries
var arr = text.split(/\b/g);
// dedupe the array into unique values
var uniqueArray = arr.filter(function (item, pos) { return arr.indexOf(item) == pos; });
// set an empty object to store our word data
var obj = {};
// for each unique word
for (var i = 0, l = uniqueArray.length; i < l; i++) {
var word = uniqueArray[i];
// use it as the basis of a new regex search
var myRe = new RegExp('\\b' + word + '\\b', 'g');
var myArray, tmpArray;
// we're going to use exec to find the word in the text
// how many times it appears
while ((myArray = myRe.exec(text)) !== null) {
if (tmpArray !== myArray[0]) {
// and we're going to add the information as values
// to the unique key in our object: how long the word is
// and the index positions it is found in in the text
// if the key isn't already there, add it.
obj[word] = { length: word.length, positions: [myArray.index]};
} else {
// otherwise push the new found location into the array
obj[word].positions.push(myArray.index);
}
tmpArray = myArray[0];
}
}
return obj;
}
getBox(text);
因此,您最终得到一个对象,该对象使用包含您将在其中找到它的文本中的每个索引的数组来标识每个单词及其长度。