I'm retreiving data from a texfile with AJAX through:

document.getElementById("my div").innerHTML=xmlhttp.responseText;

but I want to make a linebreak in the text before each capital letter starts. How do I go about doing that? Appreciate the help.

Recommended Answers

All 4 Replies

You are sure about a line break before each capital letter, right? Maybe you can try...

var str = xmlhttp.responseText;
str = str.replace(/([A-Z])/g, "<br />$1")
document.getElementById("my_div").innerHTML = str

edit: syntax error... No need to have semicolon unless you want to compact the code.

Except for line 2 I agree. Line 2 should be

str = str.replace(/([A-Z])/g, "<br />$1");

. I tested it with this string ('Hello there Mark, how is it Going.') and got this displayed.
Hello there
Mark, how is it
Going.

Thanks guys but what does the $1 stand for again?

After you use match() function, you would get an array in return if the substring is found. The $1 represents the selected sub string you want to match inside a pair of parenthesis. If you have 2 pairs, the second pair is represent by $2 and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.