Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Last active August 26, 2022 02:08
Show Gist options
  • Save nathan-cruz77/6e43ac375103de597edf52e07abfcbff to your computer and use it in GitHub Desktop.
Save nathan-cruz77/6e43ac375103de597edf52e07abfcbff to your computer and use it in GitHub Desktop.
Whatsapp text formatting
// Regex to format text to whatsapp format
function whatsToHtml(msg) {
if (!msg) return;
return msg
.replace(/~(~*[^~\n]+~*)~/g, '<del>$1</del>')
.replace(/_(_*[^_\n]+_*)_/g, '<em>$1</em>')
.replace(/\*(\**[^*\n]+\**)\*/g, '<strong>$1</strong>');
}
@caiokawasaki
Copy link

For monospace:

.replace(/`{3}(`*[^`\n]+`*)`{3}/g, "<pre>$1</pre>")

And in CSS to keep it inline:

pre {
	display: inline-block;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment