Angular 中字符串中重复单词的正则表达式,其中一个单词带有撇号

发布于 2025-01-14 01:47:05 字数 466 浏览 2 评论 0原文

我想为某个要求创建一个正则表达式,一个字符串具有重复的单词,但在该重复的单词中,一个带有撇号。

例如:

EXCHANGE CORRESPONDENCE WITH BOLNO'S COUNSEL RE SCHEDULING INTERVIEW WITH DAVID BOLNO.

我正在使用这个正则表达式通过这种方式进行验证,并用该单词分割字符串。

var splitArray = this.narrative.split(new RegExp("\\b(" + this.misspelledWords[m] + ")\\b"))

但是这个正则表达式字符串将 BOLNO 和 BOLNO'S 视为单个单词。但我想以一种应该将 BOLNO'S 和 BOLNO 视为不同的方式创建我的正则表达式。

谁能帮我这个。

I want to create a regex for a requirement, a string is having duplicate words but in that duplicate words one is with a apostrophe.

For example:

EXCHANGE CORRESPONDENCE WITH BOLNO'S COUNSEL RE SCHEDULING INTERVIEW WITH DAVID BOLNO.

I am using this regex to validate by this way and splitting the string with that word.

var splitArray = this.narrative.split(new RegExp("\\b(" + this.misspelledWords[m] + ")\\b"))

But this regex string is considering BOLNO and BOLNO'S as single word.But I want to create my regex in such a way that it should consider BOLNO'S and BOLNO as different.

Can anyone help me this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2025-01-21 01:47:05

您可以使用

new RegExp("\\b(" + this.misspelledWords[m] + ")(?!['\\w])"

这也意味着 this.misspelledWords[m] 单词不能包含特殊字符。

如果右侧紧接着有 ' 或单词字符,则 (?!['\w]) 负向前视会导致匹配失败。

You can use

new RegExp("\\b(" + this.misspelledWords[m] + ")(?!['\\w])"

This also implies the this.misspelledWords[m] word cannot contain special chars.

The (?!['\w]) negative lookahead fails the match if there is a ' or a word char immediately on the right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文