简体   繁体   中英

Regex to match words unless they have a character before them

I have this regex that detects hashtags. It shouldn't match things with letters before them, so we've got a space character at the beginning of the regex:

/( #[a-zA-Z_]+)/gm

The issue is it no longer matches words at the beginning of sentences. How can I modify this regex so that instead of matching with spaces, it simply DOESN'T match things with letters before them.

Thanks!

Use \\b at the start to indicate a word boundary . \\b won't work, since # isn't a word starter.

Just check for the start of the string or a space before: (?:^|\\s)(\\#[a-zA-Z_]+)

Also, make sure you escape the # , so it doesn't get interpreted as a comment.

Without lookbehind :

 pattern = /(?:^|[^a-zA-Z])#[a-zA-Z]+/​​​​​​​​​​​​​​​​​​

With lookbehind (but not allowed in Javascript ):

 pattern = "(?:^|(?<![a-zA-Z]))#[a-zA-Z]+"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM