Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. const regexpTree = require('regexp-tree');
  2.  
  3. // Get AST.
  4. const ast = regexpTree.parse('/[a-z]{1,}/');
  5.  
  6. // Handle nodes.
  7. regexpTree.traverse(ast, {
  8.  
  9. // Handle "Quantifier" node type,
  10. // transforming `{1,}` quantifier to `+`.
  11. onQuantifier(node) {
  12. // {1,} -> +
  13. if (
  14. node.kind === 'Range' &&
  15. node.from === 1 &&
  16. !node.to
  17. ) {
  18. node.kind = '+';
  19. delete node.from;
  20. }
  21. },
  22. });
  23.  
  24. // Generate the regexp.
  25. const re = regexpTree.generate(ast);
  26.  
  27. console.log(re); // '/[a-z]+/'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement