const regex = /[!-\/:-@[-`{-~]/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('[!-\\\/:-@[-`{-~]', 'gm')
const str = ` ! U+0021 EXCLAMATION MARK
" U+0022 QUOTATION MARK
# U+0023 NUMBER SIGN
\$ U+0024 DOLLAR SIGN
% U+0025 PERCENT SIGN
& U+0026 AMPERSAND
' U+0027 APOSTROPHE
( U+0028 LEFT PARENTHESIS
) U+0029 RIGHT PARENTHESIS
* U+002A ASTERISK
+ U+002B PLUS SIGN
, U+002C COMMA
- U+002D HYPHEN-MINUS
. U+002E FULL STOP
/ U+002F SOLIDUS
: U+003A COLON
; U+003B SEMICOLON
< U+003C LESS-THAN SIGN
= U+003D EQUALS SIGN
> U+003E GREATER-THAN SIGN
? U+003F QUESTION MARK
@ U+0040 COMMERCIAL AT
[ U+005B LEFT SQUARE BRACKET
\\ U+005C REVERSE SOLIDUS
] U+005D RIGHT SQUARE BRACKET
^ U+005E CIRCUMFLEX ACCENT
_ U+005F LOW LINE
\` U+0060 GRAVE ACCENT
{ U+007B LEFT CURLY BRACKET
| U+007C VERTICAL LINE
} U+007D RIGHT CURLY BRACKET
~ U+007E TILDE`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions