Guide · Open source
How to use JSVerbalExpressions: build regex step by step
Updated · written with AI from the project's own documentation and checked against our catalogue data
JSVerbalExpressions (published on npm as verbal-expressions) is a JavaScript library for constructing difficult regular expressions. Instead of writing regex syntax by hand, you chain readable method calls such as startOfLine, then and maybe to describe a pattern step by step. This guide shows how to install it in Node.js or the browser, how to build and test an expression, and how to use it for string replacement.
Quick start
- 1Check Node.js. Make sure Node.js is installed, since the README lists it as the prerequisite for the npm install route.
- 2Install the package. Run npm install verbal-expressions in your project folder.
- 3Load the library. Use require('verbal-expressions') or an ES6 import, and assign it to VerEx.
- 4Chain methods to describe the pattern. Start with VerEx(), then add steps such as startOfLine, then, maybe and endOfLine.
- 5Test a string. Call test() on the expression with your string, as you would with a normal RegExp.
- 6Replace text. Use find() with replace() to substitute text in a string.
What JSVerbalExpressions is and who it suits
VerbalExpressions is a JavaScript library that helps construct difficult regular expressions. Each method call adds one part of the pattern, so the code reads closer to a description of what you want to match.
It is a good fit for JavaScript developers who find regex syntax hard to read or write, for people documenting a pattern in a more readable form, for students learning how regular expressions are built, and for teams maintaining existing VerbalExpressions code. It is less suited to projects that need the smallest possible dependency, because the README does not describe its size or performance cost, and to developers who already write regex fluently.
Install JSVerbalExpressions with npm
You need Node.js. Install the package from npm with the command below. The README also shows how to load it in the browser with a script tag, or from the jsDelivr CDN.
- Browser: include VerbalExpressions.js with a script tag.
- Node.js: require the package as shown in the code block, or use ES6 import.
npm install verbal-expressionsLoad the library in Node.js
After installing, load the library with require or with an ES6 import. The README uses the name VerEx for the imported object.
const VerEx = require('verbal-expressions');
// or
import VerEx from 'verbal-expressions';Build and test a pattern: a URL check example
The README example builds an expression that checks for a correctly formed URL. Each chained call maps to a part of the pattern. The tester object can be used like a normal RegExp, so calling test() on it returns true or false.
The README notes that logging the object prints the actual regular expression it produced, which is useful for learning how method calls translate into regex syntax.
- startOfLine() anchors the match to the start of the string.
- then('http') matches the literal text http.
- maybe('s') makes the s optional.
- then('://') matches the literal separator.
- maybe('www.') makes the www. prefix optional.
- anythingBut(' ') matches any run of characters that are not spaces.
- endOfLine() anchors the match to the end of the string.
const tester = VerEx()
.startOfLine()
.then('http')
.maybe('s')
.then('://')
.maybe('www.')
.anythingBut(' ')
.endOfLine();
const testMe = 'https://www.google.com';
if (tester.test(testMe)) {
alert('We have a correct URL');
} else {
alert('The URL is incorrect');
}
console.log(tester); // Outputs: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/Replace text with find and replace
The find() method describes the text to search for, and replace() runs the expression on a string, substituting the replacement text. The README shows both a longer form and a one-line shorthand that chains find and replace together.
- Store the expression in a variable and call replace(string, replacement) to reuse it.
- Chain find() and replace() directly when you only need a single substitution.
const replaceMe = 'Replace bird with a duck';
const expression = VerEx().find('bird');
const result = expression.replace(replaceMe, 'duck');
// Outputs "Replace duck with a duck"
// Shorthand
const shorthand = VerEx().find('red').replace('We have a red house', 'blue');
// Outputs "We have a blue house"Run tests and build a minified version
The project provides npm scripts for testing and for producing a minified build. The build runs Babel on VerbalExpressions.js and writes the output to dist/verbalexpressions.js, with a minified copy at dist/verbalexpressions.min.js and a source map in dist for debugging. These steps are for people working on the library itself or building their own bundle.
npm test
# or
npm run test:verbose
npm run buildCompare with other regex builders
The README lists similar projects that also build regular expressions: re-build, regenerate and verbalregex. It also links to verbalregex.com, which the README describes as a wrapper of JSVerbalExpressions where you write code and compile it to a regex. The README also lists ports in other languages, including Python, Java, C# and Ruby.
The README does not compare performance, features or size against these alternatives, so test them on your own patterns before choosing one.
What people use it for
- Validating that a string looks like a URL, using the README's URL example.
- Replacing a word in a string, such as swapping bird for duck.
- Writing a regex in readable chained steps so teammates can follow the logic.
- Learning how method calls translate into regular expression syntax by printing the built expression.
Frequently asked questions
How do I install JSVerbalExpressions?
Install it from npm with npm install verbal-expressions. The README lists Node.js as the prerequisite for this route. You can also load VerbalExpressions.js in the browser with a script tag or from the jsDelivr CDN.
Is JSVerbalExpressions free to use?
The project is released under the MIT licence, as shown in the README and repository metadata. Check the LICENSE file in the repository for the full terms.
What do I need to run JSVerbalExpressions?
The README names Node.js as the prerequisite for installing through npm. For browser use, you only need to include the script file. The README does not list other requirements.
How do I use it in Node.js with import syntax?
After installing, use import VerEx from 'verbal-expressions' for ES6 modules, or const VerEx = require('verbal-expressions') for CommonJS. Then call VerEx() to start building an expression.
What are the alternatives to JSVerbalExpressions?
The README lists re-build, regenerate and verbalregex as similar projects. verbalregex.com is a wrapper of JSVerbalExpressions where you write code and compile it to a regex. The README does not compare them on performance or features.
Why does my expression not match what I expected?
Print the expression with console.log to see the regular expression it actually produced, as the README example does. Compare that output with the pattern you intended, since each method call adds one part of the regex. The README does not list specific troubleshooting steps.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
JSVerbalExpressions suits JavaScript developers, students and teams who find regex syntax hard to read or maintain and want patterns written as chained steps. Skip it if you need the smallest possible dependency, want performance guarantees the README does not provide, or already write regex fluently and prefer native syntax.
Install options & downloads