unjs / magic-regexp

A compiled-away, type-safe, readable RegExp alternative
https://regexp.dev
MIT License
3.67k stars 51 forks source link

Unable to convert regex with custom character set to magic-regexp #397

Open favna opened 1 month ago

favna commented 1 month ago

📚 Is your documentation request related to a problem?

I am trying to convert the regexp /[A-Za-zÀ-ÖØ-öø-ÿ]\S*/g to magic-regexp, however I cannot figure out how to capture the section of À-ÖØ-öø-ÿ. The first bit of the character set would be covered by letter, but I cannot find any way to provide a custom character set.

So far my conversion is:

const TO_TITLE_CASE = /[A-Za-zÀ-ÖØ-öø-ÿ]\S*/g;
const TO_TITLE_CASE = createRegExp('[A-Za-zÀ-ÖØ-öø-ÿ]', not.whitespace.times.any(), [global]);

However this results in the - being escaped and the resulting regexp to be:

MagicRegExp<"/[A\\-Za\\-zÀ\\-ÖØ\\-öø\\-ÿ]\\S*/g", never, [], "g">

Looking at the source code there simply does not seem to be any way to provide a custom character set, this could possibly be solved by exporting createInput.

🔍 Where should you find it?

  1. I would expect a method for custom character set to be documented on https://regexp.dev/guide/examples
  2. Possibly I would expect createInput to be exported so it can used to achieve a custom character set

ℹ️ Additional context

The regex to be converted is for this file: https://github.com/sapphiredev/utilities/blob/main/packages/utilities/src/lib/toTitleCase.ts