javascript split text after x characters
// =======( /.{4}/g ) Replace number 4 number the any the number for cout the char // this will add space after each charactar // ========= (index % 40 == 36 ? '\n' : ' ') spose you have 100 characater thrn //this line will add new line after each 40 character.. var replaced = str.replace(/.{4}/g, function (value, index) { return value + (index % 40 == 36 ? '\n' : ' '); });
Here is what the above code is Doing:
1. The replace() method takes two arguments: a regular expression and a replacement string.
2. The regular expression is /.{4}/g. This means “match any four characters”.
3. The replacement string is a function. This function takes two arguments: the matched string and the index of the match.
4. The function returns the matched string, plus a space or a newline character.
5. The index % 40 == 36 part means “if the index is a multiple of 40, add a newline character; otherwise, add a space”.