将字符串中的单词提取到数组中

时间:2018-03-06 21:19:19

标签: javascript arrays

我想从字符串中提取所有单词,然后在数组中输出它们。

“word”是指一系列连续的字母。如果我有空格或其他字符,那么这个词就会结束。

例如,如果我有这个字符串:

"my name is sil/ves tru, what?is." 

我想要一个这样的数组:

arr[0] = "my";
arr[1] = "name";
arr[2] = "is";
arr[3] = "sil";
arr[4] = "ves";
arr[5] = "tru";
arr[6] = "what";
arr[7] = "is";

这就是我目前所拥有的:

var str = "my name is sil/ves tru, what?is."; //my string
var i;
var arr = []; 
for (i = 0; i < str.length; i++) { //check all positions
    if ((str[i] >= "a") && (str[i] <= "z")) { //check where string don't have letter and put the word in arr
       //help me here
    } 
}
console.log(arr); //my array with words

6 个答案:

答案 0 :(得分:1)

您可以使用正则表达式来拆分任何连续的非字母序列。

&#13;
&#13;
var str = "my name is sil/ves tru, what?is.";
var arr = str.split(/[^a-z]+/i).filter(Boolean); // or .filter(s=>s)
console.log(arr);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用此正则表达式/[^a-z]+/gi删除不需要的字符,然后拆分。

&#13;
&#13;
var str = "my name is sil/ves tru, what?is.".replace(/[^a-z]+/gi, " ").trim().split(" ");
console.log(str);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您只需要将正则表达式与 String.replace() String.trim() String.split() 结合使用:

var str =  "my name is sil/ves tru, what?is.";
var ary = str.replace(/[^a-zA-Z ]/g, " ").trim().split(/\s+/);
console.log(ary);

/*
  ^              not
  a-zA-z         a through z or A through Z
  g              global find/replace
  
  .trim()        remove leading/trailing space from string
  .split(/\s+/)  split the string where there is one or more spaces and return an array 
*/

答案 3 :(得分:0)

这样做。使用正则表达式,除了字母之外的所有内容都会分开,如果句子没有以字母结尾,则使用filter(Boolean)删除数组中的最后一个空项。

&#13;
&#13;
let string = "my name is sil/ves tru, what?is.";

let array = string.split(/[^A-Za-z]+/).filter(Boolean);
console.log(array)
&#13;
&#13;
&#13;

答案 4 :(得分:0)

谢谢大家!!!

以下是我如何解决这个问题:

var str = "what am/i doing.      .   j  here 9what";
var i;
var j = 0;
var arr = [];
var temp = "";

for (i = 0; i < str.length; i++) {

    if ((str[i] >= "a") && (str[i] <= "z")) {
        temp = temp + str[i];
    }

    else {
        if (temp == "") {
            continue;
        }

        else {

            arr[j] = temp;
            temp = "";
            j++;
        }
    }

    if ((i == str.length - 1) && ((str[i] >= "a") && (str[i] <= "z"))) {
        arr[j] = temp;
    }
}
console.log(str.length);
console.log(arr);

答案 5 :(得分:0)

最优雅/最短的解决方案当然是使用正则表达式。

一个更好的“循环”解决方案,根据您的回答,export class AppComponent implements OnInit { constructor() { } @ViewChild(HeatmapLayer) heatmapLayer: HeatmapLayer; heatmap: google.maps.visualization.HeatmapLayer; map: google.maps.Map; points = []; ngOnInit() { this.heatmapLayer['initialized$'].subscribe(heatmap => { this.points = [ new google.maps.LatLng(7.8731, 80.7718), new google.maps.LatLng(7.8631, 80.7618), new google.maps.LatLng(7.8531, 80.758), new google.maps.LatLng(7.8431, 80.7418), new google.maps.LatLng(7.8331, 80.7318), new google.maps.LatLng(7.8231, 80.7218), new google.maps.LatLng(7.8131, 80.7118), new google.maps.LatLng(7.8111, 80.7018), new google.maps.LatLng(7.8105, 80.6918), new google.maps.LatLng(7.8100, 80.6518), new google.maps.LatLng(7.9403, 81.0188), new google.maps.LatLng(7.9803, 83.0188), ]; this.heatmap = heatmap; this.map = this.heatmap.getMap(); this.heatmap.set('opacity',0.6) }); } } 重构,使用标准if样式,并使用建议的严格比较运算符else if&amp; ===,将是:

!==

请注意,仅当最后一个字符是字母时,才需要检查输入字符串的结尾(如果已到达,则向数组添加最后一个字)。如果最后一个字符不是字母,则最后一个单词已添加到数组中,var str = "what am/i doing. . j here 9what"; var i; var j = 0; var arr = []; var temp = ""; for (i = 0; i < str.length; i++) { if ((str[i] >= "a") && (str[i] <= "z")) { temp = temp + str[i]; if (i === str.length - 1) { arr[j] = temp; } } else if (temp !== "") { arr[j] = temp; temp = ""; j++; } } console.log(str.length); console.log(arr);为空。

一个更好的循环解决方案,使用较短的赋值运算符temp,字符串+=方法允许大写字母和数组toLowerCase()方法,从而消除第二个索引变量{ {1}},是:

push()