简体   繁体   English

JS 正则表达式匹配以字符开头的单词,但如果它是双精度则忽略

[英]JS Regex match word starting with character but ignore if it's double

So I have this regular exp :: /(?:^|\W)\£(\w+)(?!\w)/g所以我有这个常规的 exp :: /(?:^|\W)\£(\w+)(?!\w)/g

This is meant to match words following the character £ .这是为了匹配字符£之后的单词。 ie; IE; if you type Something £here it will match £here .如果您输入Something £here ,它将匹配£here

However, if you type ££here I don't want it to be matched, however, i'm unsure how to match £ starting but ignore if it's ££ .但是,如果您££here我不希望它匹配,但是,我不确定如何匹配£开始但忽略它是否是££

Is there guidance on how to achieve this?是否有关于如何实现这一目标的指导?

You can add £ to \W :您可以将£添加到\W

/(?:^|[^\w£])£(\w+)/g

Actually, (?!\w) is redundant here (as after a word char, there is no more word chars) and you can remove it safely.实际上, (?!\w)在这里是多余的(因为在一个单词 char 之后,没有更多的单词字符),您可以安全地删除它。

See the regex demo .请参阅正则表达式演示 Details :详情

  • (?:^|[^\w£]) - start of string or any single char other than a word and a £ char (?:^|[^\w£]) - 字符串的开头或除单词和£字符之外的任何单个字符
  • £ - a literal char £ - 文字字符
  • (\w+) - Group 1: one or more word chars. (\w+) - 第 1 组:一个或多个单词字符。

If you also don't want to match $£here如果你也不想$£here

(?<!\S)£(\w+)

Explanation解释

  • (?<!\S) Assert a whitespace boundary to the left (?<!\S)向左断言空白边界
  • £ Match literally £从字面上匹配
  • (\w+) Capture 1+ word characters in group 1 (\w+)捕获组 1中的 1+ 个单词字符

See a regex101 demo .请参阅regex101 演示


If a lookbehind is not supported:如果不支持后视:

(?:^|\s)£(\w+)

See another regex101 demo .查看另一个regex101 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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