Click here to Skip to main content
15,885,244 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.

JavaScript
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?

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

Many thanks!
Posted
Updated 2-Feb-22 3:47am
v4

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.
 
Share this answer
 
v2
Comments
User 15221028 2-Feb-22 3:53am    
Thanks for the suggestion regarding the Expresso! It will be a good tool to learn more about regular expressions,
OriginalGriff 2-Feb-22 4:24am    
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