I need a RegEx for Javascript which will match the string “smith” in lines 1-4 but not in lines 5-8 below.
The numbered lines are fields in an Access database (the fields do not contain the numbers).
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.
How can I get the expression to also ignore the Smiths which are preceded or followed by a hyphen?
- Smith, Jones, Wilson
- Smith Jones Wilson
- Jones, Smith(Jr), Wilson
- Jones, Wilson, Smith
- Arrowsmith, Jones
- Wilson, Smithson
- Jones, Smith-Treadstone, Wilson
- 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:While
\Wmatches 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.You could of course use
\wwithin that character class: