Separate string in groups of 4 in JavaScript

This magic regex will separate a string into groups of 4 characters (or less if in the end the group is smaller)

'12345678910'.match(/.{1,4}/g).join('-')

Output: "1234-5678-910"

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*