簡體   English   中英

RegExp匹配字符串開頭或空格后的#標簽

[英]RegExp to match hashtag at the begining of the string or after a space

我查看過以前的問題和答案,但是他們沒有解決以下問題:

https://stackoverflow.com/questions/ask#notHashTag

我最接近的是: (^#|(?:\\s)#)(\\w+) ,它在一半必要的情況下找到主題標簽,並且還包括返回文本中的前導空格。 以下是需要匹配的所有案例:

#hashtag
a #hashtag
a #hashtag world
cool.#hashtag
##hashtag, but only until the comma and starting at second hash
#hashtag#hashtag two separate matches

這些應該被忽略:

https://stackoverflow.com/questions/ask#notHashTag
Word#notHashTag
#ab is too short to be a hashtag, 3 characters minimum

這應該適用於除#hashtag#duplicates所有內容,並且由於JS不支持lookbehind,因此可能無法自行匹配。

\B#\w{3,}

\\B設計為僅匹配兩個單詞字符或兩個非單詞字符。 由於#是非單詞字符,因此強制匹配前面有空格或標點符號或字符串的開頭。

試試這個正則表達式:

(?:^|[\s.])(#+\w{3,})(#+\w{3,})?

在線演示: http//regex101.com/r/kG1nD5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM