'Regex for duplicate words in a string with one of them with a apostrophe in Angular

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew