0

这就是问题所在:我有一个带有 x 元素的数组,每个元素 a 都有字符,我需要一对一地清理每个元素,结束时没有字符。

var dale = [".algo,", 'otro;', "two:", ", perfecto", "nada,", "bueno ,", "hola .", ". kcoy;", ", koko:", "noc'", " 'nocto", "lolo...", "...lola", "(jose", "luis)", "maria]", "[uranga", "signo?", "¿preg", "oki!", "¡com"];

var patterns = [".", ",", ";", ":", "'", "...", "(", ")", "[", "]", "?", "¿", "!", "¡" ];

var newDale = [];
//SOLUTIONS WRONG:
for ( var i=0; i<dale.length;i++){
  for ( var x=0; x<patterns.length;x++){
    if (dale[i].search(patterns[i])){
      newDale.push(dale[i].replace(/patterns[i]/, ""));
    }
  }
}

console.log(newDale);

//OUTPUT CLEAN 

["algo", 'otro', "two", "perfecto", "nada", "bueno", "hola", "kcoy", "koko", "noc", " 'nocto", "lolo", "lola", "jose", "luis", "maria", "uranga", "signo", "preg", "oki", "com"]
4

2 回答 2

2

I would recommend using a RegExp object to do some replacing on the strings. It looks like you want only word characters (letters), correct? Maybe something like:

for (var i=0; i < dale.length; i++){
    newDale.push(dale[i].replace(/\W/g, ""));
}

References:

http://www.w3schools.com/jsref/jsref_obj_regexp.asp (\W is non-word character, g modifier is global)

http://www.w3schools.com/jsref/jsref_replace.asp

于 2013-08-29T05:08:36.437 回答
0

Why not something like this?

var dale = [".algo,", 'otro;', "two:", ", perfecto", "nada,", "bueno ,", "hola .", ". kcoy;", ", koko:", "noc'", " 'nocto", "lolo...", "...lola", "(jose", "luis)", "maria]", "[uranga", "signo?", "¿preg", "oki!", "¡com"];

for (var i = 0; i < dale.length; i++) {
    console.log(dale[i].replace(/[.,;:;()\[\]?¿!¡ ']/g, ""));
}
于 2013-08-29T05:33:51.827 回答