gpt4 book ai didi

JavaScript 正则表达式转译器?

转载 作者:行者123 更新时间:2023-12-03 00:32:18 26 4
gpt4 key购买 nike

这个问题专门涉及转译器或转译器扩展。 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

关于JavaScript 正则表达式转译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com