0

I am trying to add a line break after ever 5 characters in a column

value = "hellohowar"

return value.toString().replace(/(.{5})/g, "$1<br/>")

Expected:

hello
howar

actual

hello<br/>howar
Kimaya
  • 1,210
  • 4
  • 14
  • 33
  • 1
    If you want to add a line break "actual" result is right where your "expected" result is wrong. If you want to match actual with expected you have to add a blank space, not a line break – Mosè Raguzzini Dec 21 '18 at 14:02
  • If you need `"hello howar "` as output, just replace the line break with an empty space. If however you need "hello howar", without a trailing space at the end, it might be easier to split, then join the string: `"hellohowar".split( /(.{5})/ ).filter( Boolean ).join( ' ' );` – Shilly Dec 21 '18 at 14:10
  • Sorry the editing messed it up. I need a line break and not a space –  Kimaya Dec 21 '18 at 14:14
  • Then you have the answer right? If you need to insert the text into HTML,
    is what you actually want. If you need it as plain text, just use \n instead of
    .
    – Shilly Dec 21 '18 at 14:18

1 Answers1

0

Try This.

var value = "Hello Howar"
value = value.replace(/(.{5})/g, exampleReplace);

function exampleReplace(str, group1, group2) {
    return group1 + '\n'; // if u want add br tag, \n change to <br/>
}