Print regular expressions with syntax highlighting in the terminal. Useful for debugging, visually inspecting and understanding complex regex patterns.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install with npm:
$ npm install --save pretty-regeximport { parse } from 'pretty-regex';
// Parse a regex pattern into a tree
const tree = parse(/foo(bar)+[a-z]/);
// Get the parsed AST
console.log(tree);
// Get colorized output
console.log(tree.print());Parse a regular expression pattern into a tree structure that can be used to create colorized output.
Params
regex{RegExp|String}: The regex pattern to parse.
Returns
{Object}: Returns a tree with nodes for each part of the regex.
Example
import { parse } from 'pretty-regex';
// Parse a string pattern
const tree = parse('foo[a-z]');
// Or pass a RegExp instance
const tree = parse(/foo[a-z]/);
// Customize colors with your own print function
// or use the built-in tree.print() method
console.log(tree.print());import util from 'node:util';
import { parse } from '~/parse';
const tree = parse(/^a(?<foo>z)[a-b]$/);
console.log(util.inspect(tree, { depth: null }));
// {
// type: 'root',
// value: '/^a(?<foo>z)[a-b]$/',
// nodes: [
// { type: 'slash', value: '/' },
// { type: 'caret', value: '^' },
// { type: 'text', value: 'a' },
// {
// type: 'paren',
// value: '(?<foo>z)',
// nodes: [
// { type: 'left_paren', value: '(' },
// { type: 'qmark', value: '?' },
// {
// type: 'angle',
// value: '<foo>',
// nodes: [
// { type: 'left_angle', value: '<' },
// { type: 'text', value: 'foo' },
// { type: 'right_angle', value: '>' }
// ]
// },
// { type: 'text', value: 'z' },
// { type: 'right_paren', value: ')' }
// ]
// },
// {
// type: 'bracket',
// value: '[a-b]',
// nodes: [
// { type: 'left_bracket', value: '[' },
// { type: 'text', value: 'a-b' },
// { type: 'right_bracket', value: ']' }
// ]
// },
// { type: 'dollar', value: '$' },
// { type: 'slash', value: '/' }
// ]
// }Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm testBuilding docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verbJon Schlinkert
Copyright © 2025, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 18, 2025.
