如何清理字符串并在Javascript中仅保留字母单词

时间:2016-10-05 10:02:44

标签: javascript split

我有用户生成的字符串,我需要“清理”它们并且只保留仅包含字母字母的单词。例如

  • “3个苹果” - > “苹果”
  • “4公斤。苹果” - > “苹果”
  • “morning 25 lim。edition” - > “早上版”(lim。不仅仅是阿尔法)

我想在“”上进行拆分,然后迭代单词,然后只在结果中添加仅包含字母的子字符串。知道如何以最有效的方式做到这一点吗?

2 个答案:

答案 0 :(得分:1)

let getAlphaWords = (words) => {
    // A case-insensitive regex to match alpha characters
    const ALPHA_REGEX = /^[a-z]+$/i;

    // A function to test if a word only contains alpha characters
    let isAlphaWord = (word) => ALPHA_REGEX.test(word);

    // Split into words and filter out all that contain non-alpha
    let alphas = words.split(' ').filter(isAlphaWord);

    // rejoin the words
    return alphas.join(' ');
}

这可以这样使用:

getAlphaWords("3 apples"); // "apples"
getAlphaWords("4 kg. apples"); // "apples"
getAlphaWords("morning 25 lim. edition"); // "morning edition"

以上是非ES6代码:

var getAlphaWords = function (words) {
    // A case-insensitive regex to match alpha characters
    var ALPHA_REGEX = /^[a-z]+$/i;

    // A function to test if a word only contains alpha characters
    var isAlphaWord = function (word) {
        return ALPHA_REGEX.test(word);
    }

    // Split into words and filter out all that contain non-alpha
    var alphas = words.split(' ').filter(isAlphaWord);

    // rejoin the words
    return alphas.join(' ');
}

答案 1 :(得分:0)

function isWord(inptWord,wordsArray){
  try{
    var wordRegex = new RegExp(/[a-z]+$/,"i");
    if(wordRegex.test(inptWord)){
      wordsArray.push(inptWord);
    }
  }catch(err){
    console.error("Err in fn. isWord : %s",err);
  }
  return wordsArray;
}

function getWordsOnly(inptString){
  var resultString = "";
  try{
    if(typeof inptString == "string"){
      var splitWords = inptString.split(" ");
      var wordsArray = new Array();
      for(var i in splitWords){
        wordsArray = isWord(splitWords[i],wordsArray);
      }
      resultString = wordsArray.join(" ");
    }
  }catch(err){
    console.error("Err in fn. getWordsOnly : %s",err);
  }
  return resultString;
}

// getWordsOnly(" morning 25 lim。edition"); ==早上版