I have a textarea, and when I write, for example, “want”, I want to replace it with “two”.
How can I match and replace whole words in javascript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the word boundary
\b, so\bwant\b. Do keep in mind that the regex definition of a word may not suit you, though. Javascript regex defines word boundaries as the place between a word character\w, which is[a-zA-Z0-9_]and a non-word character (everything except that).References
Examples of word boundary caveats
\bcan\bin"can't"(because'is not a\w)\blove\bin"love_me"(becauseeand_are both\w)