2

I want to match a separate word which starts with # character.

enter #code here - #code
some#string here - nothing
#test - #test

I came up with following regex:

   "enter #code here".replace(/\b#[\w]*/gi, "REPLACED") 

But it doesn't work. After some testing i found that

   "enter #code here".replace(/#\b[\w]*/gi, "REPLACED") 

works just fine.

Now can someone explain why \b# part is incorrect in this case?

Thanks!

2 Answers 2

3

\b is a transition between a non-word character and a word character, or vice-versa. Because a '#' is not a word character, your first expression will only match a string that has a word character directly before the #.

What might work is the expression /(\W)#[\w]*/gi, because your second example will match any of those three strings. Use a backreference to put the non-word character from the match expression into the replacement.

Sign up to request clarification or add additional context in comments.

1 Comment

If your engine supports it (Javascript doesn't), you could use a lookbehind instead of the capturing group to not capture the \W: /(?<=\W)#[\w]*/gi
1

The change between the # and [\w] is a non-word transition to word boundary, so \b matches that, but a space to # is not a boundary transition.

Your "works just fine" regex incorrectly replaces the "#string" in some#string here where you say it shouldn't: "some#string here".replace(/#\b[\w]*/gi, "REPLACED"); gave me "someREPLACED here"

What I think you want is whitespace vs. non-white with the 'sharp' (and you don't need the brackets around the \w) so the regex I came up with is

>>> "enter #code here".replace(/(\W)#\w*/gi, "$1REPLACED");
"enter REPLACED here"
>>> "some#string here".replace(/(\W)#\w*/gi, "$1REPLACED");
"some#string here"

1 Comment

Thank you for your explanation, i incorrectly understood \b character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.