admin管理员组

文章数量:1243170

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();
}

本文标签: arraysHow do I filter out a string to contain only letters in vanilla JavascriptStack Overflow