Ryan crosser Ryan crosser - 1 year ago 62
HTML Question

Add <p> tags to plain text paragraph

I am trying to write a script to automate a rather long and painful process I am required to do at work.


In short, I am given a text document that has 4 lines of text and two line breaks, and this format repeats for 5 to 8 pages. I then have to extract 6 pieces of information from each "paragraph" and type them out into a table in another document.

Since the output I get is formatted (including the fields that I extract data from), I would like to be able to paste my text into a textbox, click convert, and have it return the results I need and list them in a table.

Where I need help/advise:

From my limited JavaScript knowledge, I am thinking that I need to set

tags around each 4-line "paragraph." Then I should be able to write a script that would select the
tag, extract the data I need, write it to the table, and repeat the process for the next

I know there are online options that will do this for me (textfixer.com, for example), but my system is not connected to the internet, and for security reasons, it will never be. This means I can not use PHP (which I know better) or any other server-side processing. I am hoping this can be done through JavaScript, so I can process it through a browser. This is a Windows machine so if there is another way to do it, I am open to ideas.


You could split the document on a series of three line breaks (one to account for the end of line break, two for the empty lines), and then rejoin them with paragraph tags:

// pull the document out
var doc = $("#my-document").val();

// split on double line breaks
// note that we actually split on triple to account for the trailing
// line break at the end of a paragraph
var parts = doc.split("\n\n\n");

// rejoin with paragraph tags
doc = parts.join("</p><p>");

// wrap the entire thing in open/close paragraphs
doc = "<p>" + doc + "</p>";

// and stick it in the output

Example: http://jsfiddle.net/2uMaK/