Click here to Skip to main content
15,185,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my input field I sometimes have words that contain an apostrophe or need to be searched using quotes. Here's an example of a searched word: "de l'article".

The variable to divide words by a space is written the following way.

const r = new RegExp("(" + word.replace(/\s+/g, "|") + ")", "ig");

But this way when I'm writing a word like "de l'article", the apostrophe after the letter l doesn't seem to be taken as a word after separation.

What I have tried:

I tried to escape [^'] but it doesn't seems correct. Could you please provide any suggestions?

const r = new RegExp("(" + word.replace(/\s[^']+/g, "|") + ")", "ig");

Many thanks!
Updated 1hr ago

1 solution

You need to learn how Regexes work: \s[^']+/g specifically matches "whitespace followed by any number of characters that isn't a quote but at least one, followed by a slash, followed by a g" because looking at you question history, you are just copy'n'pasting stuff, without trying to understand how they work when they need changing.

I'd suggest that you read up on how they work: JavaScript RegExp Reference[^] and get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
User 15221028 6hrs 20mins ago
Thanks for the suggestion regarding the Expresso! It will be a good tool to learn more about regular expressions,
OriginalGriff 6hrs 5mins ago
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900