用于匹配类似 twitter 的主题标签的 Javascript 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11846975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:32:36  来源:igfitidea点击:

Javascript regex for matching twitter-like hashtags

javascriptregex

提问by ragebiswas

I'd like some help on figuring out the JS regex to use to identify "hashtags", where they should match all of the following:

我需要一些帮助来找出用于识别“主题标签”的 JS 正则表达式,它们应该匹配以下所有内容:

  1. The usual twitter style hashtags: #foobar
  2. Hashtags with text preceding: abc123#xyz456
  3. Hashtags with spacein them, which are denoted as: #[foo bar](that is, the [] serves as delimiter for the hashtag)
  1. 通常的推特风格标签: #foobar
  2. 前面带有文本的主题标签: abc123#xyz456
  3. 带有空格#[foo bar]的主题标签,表示为:(即[]作为主题标签的分隔符)

For 1 and 2, I was using something of the following form:

对于 1 和 2,我使用了以下形式的内容:

var all_re =/\S*#\S+/gi;

I can't seem to figure out how to extend it to 3. I'm not good at regexps, some help please?

我似乎无法弄清楚如何将它扩展到 3。我不擅长正则表达式,请帮忙?

Thanks!

谢谢!

回答by Felix Kling

So it has to match either all non-space characters or any characters between (and including) [and ]:

因此它必须匹配所有非空格字符或介于(包括)[和之间的任何字符]

\S*#(?:\[[^\]]+\]|\S+)

Explanation:

解释:

\S*                # any number of non-white space characters
#                  # matches #
(?:                # start non-capturing group
    \[             # matches [
    [^\]]+         # any character but ], one or more
    \]             # matches ]
    |              # OR
    \S+            # one or more non-white space characters
)                  # end non-capturing group

Reference: alternation, negated character classes.

参考交替否定字符类

回答by xiaowl

How about this?

这个怎么样?

var all_re =/(\S*#\[[^\]]+\])|(\S*#\S+)/gi;

回答by Martin Josefsson

I had a similar problem, but only want to match when a string starts and ends with the hashtag. So similar problem, hopefully someone else can have use of my solution.

我有一个类似的问题,但只想在字符串以主题标签开始和结束时进行匹配。如此类似的问题,希望其他人可以使用我的解决方案。

This one matches "#myhashtag" but not "gfd#myhashtag" or "#myhashtag ".

这个匹配“#myhashtag”但不匹配“gfd#myhashtag”或“#myhashtag”。

/^#\S+$/

/^#\S+$/

^ #start of regex
\S #Any char that is not a white space
+ #Any number of said char
$ #End of string

^ #start of regex
\S #Any char that is not a white space
+ #Any number of said char
$ #End of string

Simple as that.

就那么简单。