正则表达式:单词开头的井号

发布于 2024-12-20 20:06:44 字数 270 浏览 2 评论 0原文

我正在尝试查找以井号开头的单词。 JavaScript。

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

但单词边界似乎不适用于#符号。它输出:

test #word no<#luck>

我也有点困惑,我需要在替换模式“<#$1>”中再次添加 # 符号,因为算法似乎在此过程中将其剥离。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赴月观长安 2024-12-27 20:06:44

我是这样做的:

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

Here's how I'd do it:

result = subject.replace(/(^|\s)(#\w+)\b/g, "$1<$2>");
十秒萌定你 2024-12-27 20:06:44

试试这个:

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

\b 仅适用于 \w

Try this:

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

\b will only work with \w

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文