JavaScript Store and Load Regex as String

Nov 3, 2021

Rgex To String

const regex = /(?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{1,2})/const obj = {flags: regex.flags, source: regex.source};const string = JSON.stringify(obj);

Regex From String

const obj = JSON.parse(string);const regex = new RegExp(obj.source, obj.flags);

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.