Match literal editorial tags with JavaScript RegExp.escape
An ECMAScript 2025 exercise that matches C++, QA[Beta], and Ops (EU) literally inside an existing row grammar.
Archive retrospective · Topic year 2025
In this article
Keep editorial labels literal
An editor can assign labels such as C++, QA[Beta], and Ops (EU). Assume an export puts the content type and tag on one row, as in post:tag=C++. The type has two predefined options. The tag comes from an editorial catalogue and should match exactly. A plus sign, a bracket, or a parenthesis can mean something different in a regular expression. ECMAScript's 16th edition, published in June 2025, specifies RegExp.escape, which encodes text for literal use in a pattern. In this example only catalogue values go through that method. The row syntax is written by the program. Keeping these roles visible lets people edit the catalogue without introducing new pattern rules. If the tag has already been parsed into its own field, direct equality or set membership may represent the rule more plainly.
Make the Unicode policy explicit
The fixture does not fold letter case. It does normalize both the catalogue and input lines to NFC. This lets the same visible character match when a source writes it in a decomposed form. The first letter of İçgörü provides a small example. Case handling is a separate editorial policy, especially for dotted and dotless Turkish letters. Trimming outside whitespace is also a separate choice. If the import contract treats that whitespace as irrelevant, parse the field and deliberately apply that rule. This example expects one whole record. A trailing newline fails when included in the value passed to the matcher. A file reader can remove row delimiters before making that call. The matcher deduplicates labels after normalization. If two editorial records must remain distinct after normalization, use independent record IDs or validate those collisions during catalogue editing.
Verify seven records as a fixture
Run the script with a Node.js runtime that provides RegExp.escape. It accepts the first three lines for C++, QA[Beta], and Ops (EU), as well as the decomposed spelling of İçgörü. It rejects the shorter QAB tag, a record with an unsupported page prefix, and an otherwise matching value with a trailing newline. The output reads true, true, true, true, false, false, false. It also asserts that an empty label catalogue matches nothing. The support check makes the runtime assumption explicit before building a pattern. The entire fixture runs without network access. Each example marks a distinct detail in the accepted record shape, which makes it easier to investigate a regression. When adding another label that includes punctuation, exercise both a valid row and a similar row that should remain excluded. That confirms that the catalogue updates did not change the grammar.
const assert = require("node:assert/strict");
function makeTagMatcher(tags) {
if (typeof RegExp.escape !== "function") {
throw new Error("RegExp.escape is required");
}
if (tags.length === 0) return () => false;
const literals = [...new Set(tags.map((tag) => tag.normalize("NFC")))]
.map((tag) => RegExp.escape(tag))
.join("|");
const pattern = new RegExp("^(?:post|series):tag=(?:" + literals + ")$", "u");
return (row) => pattern.test(row.normalize("NFC"));
}
const matches = makeTagMatcher(["C++", "QA[Beta]", "Ops (EU)", "İçgörü"]);
const fixture = [
["post:tag=C++", true],
["series:tag=QA[Beta]", true],
["post:tag=Ops (EU)", true],
["post:tag=I\u0307çgörü", true],
["post:tag=QAB", false],
["page:tag=C++", false],
["post:tag=C++\n", false],
];
const observed = fixture.map(([row]) => matches(row));
assert.deepEqual(observed, fixture.map(([, accepted]) => accepted));
assert.equal(makeTagMatcher([])("post:tag="), false);
console.log(observed.join(", "));
Check the building blocks and deployment targets
The post and series alternatives belong to the program's grammar. The tag alternatives are escaped first. RegExp.escape handles more than a short list of familiar punctuation. It can also encode a leading character so that it keeps its meaning beside another pattern element. Verify the accepted values instead of requiring the escape output to look like a hand-written string. Browser and server support should be evaluated independently. The Firefox 134 developer notes list support in its January 7, 2025 release. That says when one browser shipped its implementation, while the specification date identifies the standard edition. Consider the actual devices reading or importing the archive. A support failure can be handled in an approved compatibility path, or the row can be parsed and the catalogue checked with ordinary string operations.
Measure a larger catalogue and retain diagnostic detail
An importer can compile the pattern once per catalogue and apply it to all rows in a file. With many thousands of labels, measure setup time and per-record checks separately. For inputs whose fields are already parsed, set membership may scale more simply. Before changing the method, compare the accepted and rejected sets on representative records and note the input lengths. A single Boolean suits a concise acceptance check. An editorial support report may also need to distinguish unsupported types, malformed rows, and unknown labels. Parsing fields first makes those outcomes easier to count. Keep normalisation and catalogue versions aligned between a browser preview and a server import so the same editorial entry produces the same result. Preserve a small regression fixture using typical labels and punctuation that has previously mattered to readers.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment