Skip to content
Advertisement

Replace every nth character from a string

I have this JavaScript:

JavaScript

I aim to replace every fourth letter in the string abcdefoihewfojias with |, so it becomes abc|efo|....etc,but I do not have a clue how to do this.

Advertisement

Answer

To support re-usability and the option to wrap this in an object/function let’s parameterise it:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement