Using Regex

= Regular Expressions =

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

For specialists: NovelAI uses the ECMAScript format of Regular Expressions, but only I, M, U and S are considered valid flags, and Lookbehinds are not supported.


 * Regular Expressions
 * What is a regular expression or regex?
 * A Quick Start
 * Regex Flags
 * 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 for use inside lorebook keys.

Search patterns can be used to create a myriad of condition types for checking when a key is matched, allowing the user finer control over how their lore entries are inserted into the context.

Keep in mind that Lorebook Keys must be preceded by a $ before the starting /.

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 the standard we can decipher this example and see how easy, logical and powerful the matching can be:


 * The set of / to / limit the expression in this case.
 * \ is used to escape certain symbols (the square brackets and curly braces in this example).
 * .* means match everything until the next 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 as 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. [ #top | ⬆ Return to Top]

Regex Flags
A Flag is a letter 'appended to the end' of your regular expression. It helps the parser understand how far it should check for things. Flags apply to the entire expression. You can stack any combination of flags at the end of your expression.

All flags can be disabled by appending a minus before the letter.

[ #top | ⬆ Return to Top]

Useful Key Conditions
In this segment we share and explain many expressions that are useful in lorebook keys. Remember to think of regex as conditions for the text you want to be associated to your lorebook keys! Most of these are easy to copy into your lorebook with little editing.

Positive and Negative Lookahead
First we will introduce two useful conditions that may look strange initially. Positive lookahead is used to limit matches only to terms which contain specific proceeding keywords.

? is the look ahead symbol in this case, the other two are similar to those in mathematics: = is equal to, and ! is different from.



Negative lookahead is used to deny any matches containing those keywords.



In our examples using regex101, a positive match is highlighted blue and the right sidebar gives you detailed information on the expression. Handy!

NOTE: Lookbehind conditions '''are not supported by NovelAI due to a limitation present in most browsers. As a result, do not use them.'''

Capture Groups
A capture group allows you to put apply a specific condition, but only to this group. Creating this one is simple, just encapsulate it between parentheses (like this).

This in turn leads us to:

Zero Or One Group
You define a ZOO by putting a ? after the character or capture group. When you define a ZOO, it becomes an optional match. This is useful for nicknames or titles that you often shorten down. Here is an example:

You'll notice there is a space in the group: this is mostly for cleanliness, because otherwise it will check for  which, while usually not an issue, isn't very elegant. [ #top | ⬆ Return to Top]