JavaScript 正则表达式转换器?

JavaScript regex transpiler?

这个问题专门针对转译器或转译器扩展。 RegExp() 是对象创建是 massively slower and constructs the RegExp on each iteration.

似乎 JavaScript 不允许在其正则表达式中使用不敏感的空格。是否有任何 JavaScript 的转译器允许在正则表达式中来自 perl 的 /x 之类的东西。

/x and /xx A single "/x" tells the regular expression parser to ignore most whitespace that is neither backslashed nor within a bracketed character class. You can use this to break up your regular expression into more readable parts. Also, the "#" character is treated as a metacharacter introducing a comment that runs up to the pattern's closing delimiter, or to the end of the current line if the pattern extends onto the next line. Hence, this is very much like an ordinary Perl code comment. (You can include the closing delimiter within the comment only if you precede it with a backslash, so be careful!)

这允许你写类似

的东西
let res = name.match(/^([^\d]*?)(?:\s*[.,]\s*([0-9]+)(?: mo)?)?[.,]?$/);

像这样,

let res = name.match(/
  ^
  ([^\d]*?)                       # name
  (?:\s*[.,]\s*([0-9]+)(?: mo)?)? # age, mo
  [.,]?                           # trailing dirt
  $
 /);

我相信您正在寻找完全像这样的东西:https://www.npmjs.com/package/babel-plugin-transform-modern-regexp