Using Regex

= Regular Expressions =

This section explains what a regular expression is and how they can be utilized inside NovelAI to create powerful conditions for your lore or outside of the service for editing your stories or documents for datasetting or other beneficial ends.


 * Regular Expressions
 * What is a regular expression or regex?
 * A Quick Start
 * Useful key conditions

-

What is a regular expression or regex?
A regular expression (often shortened as regex or regexp) is simply a sequence of characters that specifies a search pattern. This concept is often used in computer science or information processing. A powerful regex syntax has been a feature of JavaScript since ECMAScript6 and is implemented in NovelAI to currently offer the use of regex inside lorebook keys.

Search patterns can be used to create powerful conditions for when a key is matched, allowing the user control over how their lore entries are inserted into the context limited only by their imagination.

A Quick Start
Seeing regex for the first time can be daunting. Many are first exposed to the syntax and scared off by seeing an expression like /\[\{.*\}\]/ which looks like gibberish to most people.

However by reading on the standard we can decipher this example and see how easy, logical and powerful the matching can be. The set of // limit the expression in this case. \ is used to escape certain characters. .* means match everything until the character. Piecing all this information together, we figure out this expression will match any word inside [{}] brackets.

It isn't recommended to start learning the syntax alone when since many powerful sources and tools exist to help the journey. Among the most popular is regex101, a website that offers a search menu with examples for many simple and popular expressions, alongside an interpreter to test your expressions in multiple regex implementations.

Another useful website for dipping your feet in deeper is regular-expressions.info. It teaches you everything you need and then some. Your learning journey can also be gamified with interactive lessons like regexone or games like regexcrossword.

Useful key conditions
In this segment we share and explain many expressions that are useful in lorebook keys. Remember to think of regex as the conditions for the words and sub-strings you want to match! Most of these are easy to copy into your lorebook with little editing.

Negative lookbehind and negative lookahead
First we will introduce two powerful and useful conditions that may look strange first. Negative lookbehind is used to deny matches for preceding words, while negative lookahead is used to deny matches for proceeding words. Examples from regex101.



Here we see that on regex101 a positive match is blue and that the right sidebar gives you detailed information on the expression. Handy!