I have a textarea.I want when I write ,for example "want", to replace it with "two". How to match and replace whole words in javascript?
+2
A:
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
- There's a
\bcan\bin"can't"(because'is not a\w) - There's no
\blove\bin"love_me"(becauseeand_are both\w)
polygenelubricants
2010-06-20 16:48:54
`\blove\b\w` will never match, because `e\b\w` will never match, because there's no `\b` between `e` and `\w`, which are both `\w`.
polygenelubricants
2010-06-20 16:58:37
Thank you very much !
lam3r4370
2010-06-20 16:59:30