Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const regexpTree = require('regexp-tree');
- // Get AST.
- const ast = regexpTree.parse('/[a-z]{1,}/');
- // Handle nodes.
- regexpTree.traverse(ast, {
- // Handle "Quantifier" node type,
- // transforming `{1,}` quantifier to `+`.
- onQuantifier(node) {
- // {1,} -> +
- if (
- node.kind === 'Range' &&
- node.from === 1 &&
- !node.to
- ) {
- node.kind = '+';
- delete node.from;
- }
- },
- });
- // Generate the regexp.
- const re = regexpTree.generate(ast);
- console.log(re); // '/[a-z]+/'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement