A
A
Arthur2020-01-29 00:05:09
JavaScript
Arthur, 2020-01-29 00:05:09

How to substitute "-" every 4 characters?

I need the string [a-zA-Z0-9], for example ABCDEF2GA3BCABCD to be converted to ABCD-EF2G-A3BC-ABCD.

Having messed up the regular expression, I could only get the first 4 separated by a hyphen, and then each character was separated.
I need the string to be split with a hyphen every 4 characters, with a tolerance of [a-zA-Z0-9].

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-01-29
@arturka_v_10

str.replace(/\w{4}(?!$)/g, '$&-')
or
str.match(/\w{1,4}/g).join('-')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question