javascript - 一次允许一个特定字符的正则表达式
问题描述
我有一个要求,允许任何数字一次只有一个字符(W 或 D),没有顺序。
例如:2w或222W或2d或222D或2w5d
(不像2wd,55dw)_
我试过这样的东西,但它不工作
[^\\d][w]$.
解决方案
这是你想要的?
/\b(\d+[dw](?![dw]))+\b/ig
编辑:请参阅上面评论中@bobblebubble 的答案以获得更好的选择。
请参阅下面或此处的片段:https ://regex101.com
const regex = /\b(\d+[dw](?![dw]))+\b/ig;
const str = `2w 55dw 222W 2d 2wd 222D 2w5d`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
document.write(`Found match: ${m[0]}<br>`);
}
推荐阅读
- asp.net - UserManager 的 ResetPasswordAsync 方法的详细信息
- c++ - 有没有办法改变 Pytorch 中的卷积方法?(或者在其他框架中?)
- javascript - SyntaxError:函数语句需要函数名
- c++ - 如何附加到 Makefile 中的变量列表?
- bash - Bash 语法:如果 [ ! -F \”{}\” ]; 然后退出1;菲'
- asp.net - 从我的前端而不是从硬核编码 Asp.net 制作多列 gridview readyonly=true,false
- c++ - 将“__m256 with random-bits”转换为 [0, 1] 范围的浮点值
- android - 小米手机默认开启特殊权限
- jestjs - React 测试库 - 全局配置
- mysql - MySQL两列查询检查记录