I have an expression in JS to match all the words containing (for example) 'foo+', but I would like to discard those of them who end with some other pattern.

Is there a way to make this in only one regular expression?

example:

myExp = /foo+\w*/; /*should add something to avoid ending with 'ba+r'*/

myExp.matchAll('boo, fo, food, foobaaar, foobr');

should result in ["food", "foobr"] not including 'foobaaar'

推荐答案

You want to use a negative lookahead like (?!not_this), of course replace not_this with the appropriate pattern to exclude using word-break characters \b, or whatever fits your logic.

let myExp = /\bfoo+(?!\w*ba+r\b)\w*/g;

console.log(...' boo fo food foobaaar foobr foobaardd boofoo foodbar '.matchAll(myExp));

Javascript相关问答推荐

如何在Angular中插入动态组件

按下同意按钮与 puppeteer 师

未捕获错误:[]:getActivePinia()被调用,但没有活动Pinia.🍍""在调用app.use(pinia)之前,您是否try 使用store ?""

Promise Chain中的第二个useState不更新

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

在react JS中映射数组对象的嵌套数据

获取Uint8ClampedArray中像素数组的宽度/高度

更改预请求脚本中重用的JSON主体变量- Postman

JQuery Click事件不适用于动态创建的按钮

未捕获的运行时错误:调度程序为空

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

使用Reaction窗体挂钩注册日历组件

构建器模式与参数对象输入

本地损坏的Java脚本

在传单的图像覆盖中重新着色特定 colored颜色 的所有像素

使用python,我如何判断一个html复选框是否被隐藏,以及它是否被S选中?

相对于具有选定类的不同SVG组放置自定义工具提示

带元素数组的Mongo聚合

正在发出错误的URL请求

如何使用Angular JS双击按钮