简体   繁体   English

Javascript RegEx 匹配没有前后连字符的单词

[英]Javascript RegEx to match words without preceding or trailing hyphens

I need a RegEx for Javascript which will match the string "smith" in lines 1-4 but not in lines 5-8 below.我需要一个 Javascript 的正则表达式,它将匹配第 1-4 行中的字符串“smith”,但不匹配下面的第 5-8 行。
The numbered lines are fields in an Access database (the fields do not contain the numbers).编号的行是 Access 数据库中的字段(字段不包含数字)。
What I have so far is:到目前为止,我所拥有的是:

var xy = 'smith';
MyString = new RegExp('(^|\\W)' + xy + '(\\W|$)', 'i');

This matches the Smiths in lines 1-4, but also those in Lines 7 and 8.这与第 1-4 行中的 Smiths 匹配,也与第 7 行和第 8 行中的 Smiths 匹配。
How can I get the expression to also ignore the Smiths which are preceded or followed by a hyphen?我怎样才能让表达式也忽略前面或后面有连字符的 Smiths?

  1. Smith, Jones, Wilson史密斯、琼斯、威尔逊
  2. Smith Jones Wilson史密斯琼斯威尔逊
  3. Jones, Smith(Jr), Wilson琼斯,史密斯(JR),威尔逊
  4. Jones, Wilson, Smith琼斯、威尔逊、史密斯
  5. Arrowsmith, Jones阿罗史密斯,琼斯
  6. Wilson, Smithson威尔逊,史密森
  7. Jones, Smith-Treadstone, Wilson琼斯、史密斯-崔德斯通、威尔逊
  8. Wilson, Blakely-Smith, Jones威尔逊、布莱克利-史密斯、琼斯

After a week and dozens of attempts, I am asking the experts经过一周和几十次尝试,我在问专家

Write the negative character class ( \W ) yourself and include the hyphen:自己写否定字符 class ( \W ) 并包括连字符:

MyString = new RegExp('(^|[^a-z0-9_-])' + xy + '([^a-z0-9_-]|$)', 'i');

While \W matches any non-word character (everything but letters, digits and underscores), [^a-z0-9_-] matches any character that is neither such a word character nor a hyphen. \W匹配任何非单词字符(除了字母、数字和下划线之外的所有字符),而[^a-z0-9_-]匹配任何既不是单词字符也不是连字符的字符。

You could of course use \w within that character class:您当然可以在该字符 class 中使用\w

MyString = new RegExp('(^|[^\\w-])' + xy + '([^\\w-]|$)', 'i');

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

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