I would like to know how can I split a long string to an array of words. I would like to ignore/remove all the non alphabethic characters.
For example: If I have the next string: "free games @ somewhere, visit us. don't want to miss out?, then go ahead & visit us @ somewhere-to-download-from."
I would like it to be splitted to: "free,games,somewhere,visit,us,don't,want,to,miss,out,then,go,ahead,visit,us,somewhere,to,down,load,from"
In the end I will have an array size 20 that on each cell it holds one word of the above.
Try:
var chk = str.split(/[^a-z']+/i);
console.log(chk);
Use the Javascript split
function with a regular expression. Example:
var str = "free games @ somewhere, visit us. don't want to miss out?, then go ahead & visit us @ somewhere-to-download-from.";
alert(str.split(/[^a-z]+/i));
This should work:
str = "free games @ somewhere, visit us. don't want to miss out?, then go ahead & visit us @ somewhere-to-download-from.";
str = str.replace(/[^a-z]/gi," ").replace(/ {1,}/g," ").split(" ");
document.write(str);
function specialSplit(str){
var reqArray = new Array();
var len = str.length;
var tempStr = "";
for(var i=0;i<len;i++){
if(str[i].search(/[a-zA-Z']/)==0){
tempStr = tempStr + str[i];
}
else if(tempStr.length > 0){
reqArray.push(tempStr);
tempStr = "";
}
}
return reqArray;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.