I have this string of text: "Hello my name is Andy Hello my name is John Hello my name is Matt" I want this: Hello my name is Andy Hello my name is John Hello my name is Matt How do I do this in Javascript?
Not the most elegant way I'm sure but this works: string = "Hello my name is Andy Hello my name is John Hello my name is Matt"; while (string.indexOf(" Hello") != -1) string = string.replace(" Hello", "\nHello"); alert(string); Code (markup): It finds "Hello" with a space before it and replaces the space with "\n" (\n represents a new line)
the easiest answer is to put a \n in between the phrases manually if you're allowed. that's the code for a newline. Alternatively, you could set up a function that adds a new line after every 5th word.