Home > Enterprise >  Regex expression with space does not compatible minify javascript
Regex expression with space does not compatible minify javascript

Time:02-21

I'm using this regex

/(NaN| {2}|^$)/.test(a)

It work perfectly in developer mode. In production mode, javascript would be minified, so the white space has been removed, the regex would be

/(NaN|{2}|^$)/.test(a) => wrong

How do I remain correct above regex with minify javascript?

CodePudding user response:

You can match a space using a regex escape like

\x20
\u0020

See a demo below:

const a = "-  -";
console.log(/(NaN| {2}|^$)/.test(a));
console.log(/(NaN|\x20{2}|^$)/.test(a));
console.log(/(NaN|\u0020{2}|^$)/.test(a));

  • Related