首页 > 解决方案 > Javascript过滤包含数组中数字的单词

问题描述

如何从包含数字的数组中获取所有单词?

var str = ["m2e", "you", "U2", "they#2", "someone"];
var filtered = str.filter(function (i) {
  // return i.includes('/\d/');
});

console.log(filtered); // m2e, U2, they#2

我尝试的任何方法都不起作用。你能帮我吗?

标签: javascript

解决方案


var str = ["m2e", "you", "U2", "they#2", "someone"];
var onlyString = str.filter(function(i) {
  if (!/\d/.test(i)) {
    return i;
  }
});
console.log("onlyString");
console.log(onlyString);
var withNumericString = str.filter(function(i) {
  if (/\d/.test(i)) {
    return i;
  }
});
console.log("with Numeric String");
console.log(withNumericString);


推荐阅读