I would very much appreciate some assistance from the community on replacing a string at xth position intervals, using javascript regex. For example, if the string length is 161 and the replacement text is <br />, regex would replace the string at the 40th, 80th, 120th, and 160th positions with this replacement text. Is this possible using regex?
Thank you very much.
A method to add
<br />at ever 40th position is by using the following line:If you want to dynamically set the position use:
Explanation of the code:
replacefunction is a RegExp:[\S\s]= all non-whitespace and white-space characters = every character).{40}= 40 charactersgflag means: global match, ie: match every possible occurence$1(first group)replacefunction contains$1<br />. That is: replace the full match by the first group ($1), and add<br />to it.