当明显没有匹配时,JavaScript 正则表达式测试在控制台中返回“true”

问题描述 投票:0回答:0

我正在尝试对我在 ChatGPT 的帮助下编写的 JavaScript 正则表达式进行简单测试(我几乎不懂 JavaScript!)。这是正则表达式:

new RegExp(`(i'm [a-z]+ to ${I_OPINION_WORDS})`, 'i')

一个典型的表达是“我倾向于认为……”

当我使用不包含“i'm”或“to”(“我认为我们应该重新考虑我们用来讨论这个问题的语言。”)的字符串进行测试时,测试返回 true。 有人可以帮助我理解这种行为,我猜,将正则表达式更改为返回 false 吗?

const DONT_PHRASES = / dont| don't| do not| can not| cant| can't/;
const PRONOUNS = /he|she|it|they/i;
const PRESIDENT_NAMES = /candidate|clinton|donald|gop|hillary|hilary|trump|trum/i;
const SKIP_WORDS = / also| really| very much/;

const AMBIGUOUS_WORDS = /seemed|prefer/;
const I_OPINION_WORDS = /agree|believe|consider|disagree|hope|feel|felt|find|oppose|think|thought|support/;
const OPINION_PHRASES = /in my opinion|it seems to me|from my perspective|in my view|from my view|from my standpoint|for me/;
const OPINION_PHRASE_REGEXES = [
  new RegExp(`(i(?:${DONT_PHRASES}|${SKIP_WORDS})? ${I_OPINION_WORDS})`, 'i'),
  new RegExp(`(i'm [a-z]+ to ${I_OPINION_WORDS})`, 'i'),
  new RegExp(`(${OPINION_PHRASES},? )`, 'i')
];

const regex = new RegExp(OPINION_PHRASE_REGEXES[1]);
console.log(regex.toString())
console.log(regex.test("I think we should reconsider the language we use to discuss this."));

javascript regex string-interpolation
© www.soinside.com 2019 - 2024. All rights reserved.