te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>arrays - How do I filter out a string to contain only letters in vanilla Javascript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How do I filter out a string to contain only letters in vanilla Javascript? - Stack Overflow

programmeradmin3浏览0评论

I was given this string:

var myMessage = "Learning is fun!"

This is how I attempted to create an array listing only the letters (without the spaces and "!").

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
    let arr2 = []
    for(let i = 0; i < array.length; i++){
        if(array[i] === "a" || "b" || "c" || "d" || "e" 
        || "f" || "g" || "h" || "i" || "j" || "k" || "l" 
        || "m" || "n" || "o" || "p" || "q" || "r" || "s" 
        || "t" || "u" || "v" || "w" || "x" || "y" || "z"){
          arr2.push(array[i])
        }
    }
    return arr2
}

console.log(onlyLetters(myMessage))

What am I doing wrong? Also, is there a shorthand for listing letters "a" through "z"?

I was given this string:

var myMessage = "Learning is fun!"

This is how I attempted to create an array listing only the letters (without the spaces and "!").

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
    let arr2 = []
    for(let i = 0; i < array.length; i++){
        if(array[i] === "a" || "b" || "c" || "d" || "e" 
        || "f" || "g" || "h" || "i" || "j" || "k" || "l" 
        || "m" || "n" || "o" || "p" || "q" || "r" || "s" 
        || "t" || "u" || "v" || "w" || "x" || "y" || "z"){
          arr2.push(array[i])
        }
    }
    return arr2
}

console.log(onlyLetters(myMessage))

What am I doing wrong? Also, is there a shorthand for listing letters "a" through "z"?

Share Improve this question asked Oct 6, 2019 at 17:20 Yumi ParkYumi Park 411 gold badge1 silver badge4 bronze badges 1
  • You can't do array[i] === "a" || "b" || .... because the right side of the === is evaluated as "a" || "b" || .... . What you mean to do is array[i] === "a" || array[i] === "b" || ... – ninesalt Commented Oct 6, 2019 at 17:23
Add a ment  | 

4 Answers 4

Reset to default 7

A simple way may be to use Regex like so

let message = "Learning is fun!";
let onlyLettersArray = message.split('').filter(char => /[a-zA-Z]/.test(char));
console.log(onlyLettersArray)

.filter takes an array and runs a function on the elements, which returns true or false. The item is removed if it returns false. The regex checks if the character is within the range a-z or A-Z

Another way is to filter the char and then split it like so

let message = "Learning is fun!";
let onlyLettersArray = message.replace(/[^a-z]+/gi, '').split('');
console.log(onlyLettersArray)

Edit:

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
    let arr2 = []
    for(let i = 0; i < array.length; i++){
        if(/[a-z]/.test(array[i])){ // you can use regex instead of all characters
          arr2.push(array[i])
        }
    }
    return arr2
}

console.log(onlyLetters(myMessage))

Update: If instead of an array of characters, you have to replace special chars in a string, you can write

let message = "Learning is fun!";
let letterMessage = message.replace(/[^a-zA-Z]/gm,"")
console.log(letterMessage)


You simply can use String.prototype.match to get the array of letters only.

let arr = "Learning is fun!  1233  ashdgahsgdh".match(/[A-Za-z]/g);
console.log(arr)

You could take a lower case string, split it and check each character if the value is greater or equal to 'a' and smaller or equal to 'z'.

Ath the end, after filtering, you could get a string back by joining the array with an empty string as glue.

function onlyLetters(string) {
    return string
        .toLowerCase()
        .split("")
        .filter(c => c >= 'a' && c <= 'z')
        .join('');
}

console.log(onlyLetters("Learning is fun!"));

Since OP is a beginner, I would propose another solution that doesn't use regex

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
let arr2 = []
for(let i = 0; i < array.length; i++){
    if(isLetter(array[i])){
      arr2.push(array[i])
    }
}
   return arr2
}

function isLetter(c) {
   return c.toLowerCase() != c.toUpperCase();
}
发布评论

评论列表(0)

  1. 暂无评论