سؤال

I am trying to find words starting with a pound sign. Javascript.

"test #word no#luck".replace( /\b#([\w]+)\b/g, "<#$1>" );

yet the word boundary doesn't seem to apply to the #-sign. it outputs:

test #word no<#luck>

also i am a bit confused that i need to add the #-sign again in the replacement pattern "<#$1>", as the algorithm seems to strip it in the process.

هل كانت مفيدة؟

المحلول

Here's how I'd do it:

result = subject.replace(/(^|\s)(#\w+)\b/g, "$1<$2>");

نصائح أخرى

Try this:

result = subject.replace(/(?:^|\s)(#\w+)(?:\s|$)/g, "<$1>");

\b will only work with \w

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top