so I know \bBlah\b will match a whole Blah, however it will also match Blah in “Blah.jpg” I don’t want it to. I want to match only whole words with a space on either side.
Share
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 try:
\sBlah\s.Or if you allow beginning and end anchors,
(^|\s)Blah(\s|$)This will match
"Blah"by itself, or eachBlahin"Blah and Blah"See also
\sstands for “whitespace character”.^matches the position before the first character in the string$matches right after the last character in the stringLookahead variant
If you want to match both
Blahin"Blah Blah", then since the one space is “shared” between the two occurrences, you must use assertions. Something like:See also
Capturing only
BlahThe above regex would also match the leading whitespace.
If you want only
Blah, ideally, lookbehind would’ve been nice:But since Javascript doesn’t support it, you can instead write:
Now
Blahwould be captured in\1, with no leading whitespace.See also