Awesome
regex-railroad.nvim
regex-railroad.nvim
generates useful text and diagrams to help explain regular expressions in your code.
Getting started
Required dependencies
- nvim-treesitter/nvim-treesitter to extract the regular expression text
Installation
lazy.nvim
-- plugins/regex-railroad.lua:
return {
"rclawlor/regex-railroad.nvim",
tag = "0.0.3",
dependencies = { "nvim-treesitter/nvim-treesitter" }
}
Usage
Use :RegexText
to generate a text description of the regular expression under your cursor, or :RegexRailroad
to instead generate a railroad diagram!
To remap the functions to something more convenient, use the following:
vim.api.nvim_set_keymap("n", "<C-x>", "<cmd>RegexText<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<C-s>", "<cmd>RegexRailroad<CR>", {noremap = true, silent = true})
Customisation
This section explains the available options for configuring regex-railroad.nvim
Setup function
require("regex-railroad").setup({
--- Github release of plugin
tag = "v0.0.3",
--- Highlight group used in :RegexText
highlight = {
bold = true,
fg = "fg",
bg = "bg"
}})
Supported Features
Character classes
<center>Feature | Example | Supported |
---|---|---|
Character set | [ABC] | ✓ |
Negated set | [^ABC] | ✓ |
Range | [A-Z] | ✓ |
Dot | . | ✓ |
Word | \w | ✓ |
Non-word | \W | ✓ |
Digit | \d | ✓ |
Non-digit | \D | ✓ |
Whitespace | \s | ✓ |
Non-whitespace | \S | ✓ |
Unicode category | \p{L} | ✗ |
Non-unicode category | \p{L} | ✗ |
Unicode script | \p{Han} | ✗ |
Non-unicode script | \P{Han} | ✗ |
Anchors
<center>Feature | Example | Supported |
---|---|---|
Beginning | ^ | ✓ |
End | $ | ✓ |
Word boundary | \b | ✗ |
Non-word boundary | \B | ✗ |
Groups & References
<center>Feature | Example | Supported |
---|---|---|
Capturing group | (ABC) | ✓ |
Named capturing group | (?<name>ABC) | ✓ |
Numeric reference | \1 | ✗ |
Non-capturing group | (?:ABC) | ✓ |
Lookaround
<center>Feature | Example | Supported |
---|---|---|
Positive lookahead | (?=ABC) | ✗ |
Negative lookahead | (?!ABC) | ✗ |
Positive lookbehind | (?<=ABC) | ✗ |
Negative lookbehind | (?<!ABC) | ✗ |
Qualifiers & Alternation
<center>Feature | Example | Supported |
---|---|---|
Plus | + | ✓ |
Star | * | ✗ |
Quantifier | {1,3} | ✓ |
Optional | ? | ✓ |
Lazy | ? | ✗ |
Alternation | | | ✓ |