在JavaScript中,除非单词列在被排除的单词列表中,否则如何使用正则表达式进行匹配? [英] In JavaScript, how can I use regex to match unless words are in a list of excluded words?

查看:113
本文介绍了在JavaScript中,除非单词列在被排除的单词列表中,否则如何使用正则表达式进行匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了某些单词列表外,如何使用正则表达式匹配任何单词(\ w)?例如:

How do I use regex to match any word (\w) except a list of certain words? For example:

我想匹配单词使用利用以及之后的任何单词,除非单词是某事 fish

I want to match the words use and utilize and any words after it except if the words are something or fish.

use this  <-- match
utilize that  <-- match
use something   <-- don't want to match this
utilize fish <-- don't want to match this

怎么做我指定了一个我不想匹配的单词列表?

How do I specify a list of words I don't want to match against?

推荐答案

您可以使用否定前瞻来确定你要匹配的词不是特别的事。您可以使用以下正则表达式执行此操作:

You can use a negative lookahead to determine that the word you are about to match is not a particular thing. You can use the following regex to do this:

(use|utilize)\s(?!fish|something)(\w+)

这将匹配use或utilization后跟一个空格,然后如果下面的单词不是fish或something,它将匹配下一个单词。

This will match "use" or "utilize" followed by a space, and then if the following word is not "fish" or "something", it will match that next word.

这篇关于在JavaScript中,除非单词列在被排除的单词列表中,否则如何使用正则表达式进行匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆