Javascript: regex to capture spaces and special characters. EXCLUSION A hyphen inside words

Currently using /[\W]+/gall characters other than the words I want are displayed.

However, I would like to omit hyphens when they are not surrounded by spaces (i.e. when they are used as connecting words).

Examples:

 var test = [
    'e-mail', // Nothing  shouldn't be captured
    'e-commerce is great.', // Spaces and full stop should be captured
    'He - yes he - went', // Dashes and white space should be captured
    'He&-you, me-him' // &-, and the whitespace should be captured while the dash in me-him should not
    ]
+4
source share
1 answer

Try using ([\W]-|-[\W]|[^\w-])+

, lookaheads , . , ([\W]), , ([^\w-]), , ([\W]- -[\W]). , -, , .

+2

Source: https://habr.com/ru/post/1606421/


All Articles