Home > Net >  i was trying to filter only numbers that are mixed up with letters but i can't figure out why m
i was trying to filter only numbers that are mixed up with letters but i can't figure out why m

Time:10-07

why is my code not working?

  const word = "abcd123";
  const filteredNum = word.split("").filter((e) => Number(e) !== NaN);
  console.log(filteredNum);

CodePudding user response:

Try this:

const word = "abcd123";
const result = word.match(/\d /)[0];

console.log(result);

CodePudding user response:

using isNan() function

    let word = "abcd123";
    let filteredNum = word.split("").filter((e) => !isNaN(e));
    console.log(filteredNum);
    console.log(filteredNum.join("")) 

  • Related