javascript - 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
我尝试的任何方法都不起作用。你能帮我吗?
解决方案
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);
推荐阅读
- ios - SwiftUI 组中的第一个对象重叠
- node.js - 如何使用 Koa.js 制作 API?如果用户没有登录,只能使用10次。如果用户登录成功,就可以无限次使用
- delphi-10.3-rio - 对手动创建的组件使用事件处理程序
- javascript - JS:返回一个数字的指定 10 次幂
- javascript - 我想我已经调用了那个函数,但是函数名旁边没有括号
- c++ - 这种错误的原因是什么:将“功能”重新定义为不同类型的符号
- python - 旋转包含字符串的 Pandas 数据框 - 仅在第一次添加列,否则在任何列中添加值
- java - Spring Boot JPA - ManyToOne 关系导致额外的 sql
- javascript - 当我将变量传递给函数并在内部修改时,变量不会改变
- c++ - C/C++ 在 x86 应用程序中使用 x64 结构参数导入 WinAPI 函数