javascript - 过滤器编号后跟数组中的点
问题描述
我想摆脱数组中后跟点的数字。
我尝试过这样的事情。
function parseMoves(){
const pgnmoves = ["1.", "Nf3", "Nc6", "2.", "Bc4", "e6", "3."] // And so on.
const reg = new RegExp('[0-9]+\.');
const filtered = pgnmoves.filter((x) => {
return x != reg.test(x)
})
return filtered;
}
但这似乎不起作用,我对正则表达式不太擅长。
这是预期的输出:
["Nf3", "Nc6", "Bc4", "e6"]
谢谢你的帮助!
解决方案
你的正则表达式很好。您需要使用!
运算符保留未通过测试的项目:
function parseMoves(){
const reg = /[0-9]+\./; // /^[0-9]+\.$/ - if you want to remove just items that start with a number and have a single dot at the end
return pgnmoves.filter((x) => !reg.test(x));
}
const pgnmoves = ["1.", "Nf3", "Nc6", "2.", "Bc4", "e6", "3."]; // And so on.
const result = parseMoves(pgnmoves);
console.log(result);
推荐阅读
- sql - SQL Server:将 nvarchar 转换为日期
- kotlin - 如何在 Kotlin 中使用另一个类的变量?
- javascript -
连接并放入反应组件状态变量时,标记和\ n不起作用 - linux - 如何将通配符参数传递给 bashrc 函数
- c++ - 在派生类中将公共父析构函数设为私有
- java - 无法在 kotlin 中调用静态华丽方法
- c# - 在 Google Drive Api v3 的同一个批处理请求中复制文件并添加权限
- c# - Selenium SendKeys Keys.ArrowRight 移动到 contenteditable 文本的末尾
- r - 使用索引对一系列列名进行子集化时出错
- ios - Swift - 创建自定义视图的不同实例