Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Arrow Trim and format plain text

    I have the following script that converts line breaks from plain text into HTML formatted paragraphs. It takes plain text from one text area field and outputs the new formatted text into another text area field.

    Code:
    function convertText(){
    
    var noBreaks = document.getElementById("oldText").value;
    noBreaks = noBreaks.replace(/\r\n/g,"[-LB-]");
    
    re4 = /\[-LB-\]\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re4,"</p><p>");
    
    re5 = /\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re5," ");
    
    noBreaks ='<p>'+noBreaks+'</p>';
    
    noBreaks = noBreaks.replace(/<\/p><p>/g,"</p>\r\n\r\n<p>");
    
    document.getElementById("newCode").value = noBreaks;
    
    }
    
    
    
    <textarea id="oldText" name="oldText" rows="12" cols="90"></textarea>
    
    <textarea id="newCode" name="newCode" rows="12" cols="90"></textarea>
    
    <input type="button" value="Convert" onclick="javascript:convertText()">
    1. How can I trim/filter all double-spaces, triple-spaces, and so on? I only want single spaces to exist. Plus, is there a way to remove these spaces from the very begininng and end of each paragraph?

    2. Secondly, if there's a space (not a line break or carriage return) between each paragraph, it merges the two paragraphs together, instead of creating two separate paragraphs. Is there a way to fix this?

    Thanks!

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    1.) For a maximum of one consecutive space, add
    Code:
    noBreaks = noBreaks.replace(/ +/g, " ");
    For removal of spaces at the beginning and end of paragraphs, add
    Code:
    noBreaks = noBreaks.replace(/<p> *(.*?) *<\/p>/g, "<p>$1</p>");
    Also, you may want to change the first one to
    Code:
    noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    so it matches both types of line breaks independently.

    2.) Don't know what you mean there. If there's a space and not a line break, there's no new paragraph, so why should your script create one?

  • Users who have thanked venegal for this post:

    four0four (05-09-2009)

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Thanks! Those work great.

    Quote Originally Posted by venegal View Post
    Also, you may want to change the first one to
    Code:
    noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    so it matches both types of line breaks independently.
    I tried this, but it kept adding empty <p></p> tags between each paragraph.

    2.) Don't know what you mean there. If there's a space and not a line break, there's no new paragraph, so why should your script create one?
    For example, if there's two paragraphs that look like this:

    plain text paragraph1
    [single or multiple spaces]
    plain text paragraph2

    it outputs the following:

    <p>plain text paragraph1 plain text paragraph2</p>

    ...instead of:

    <p>plain text paragraph1</p>

    <p>plain text paragraph2</p>


    Thanks again!

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Ok, I see now what you mean. But you can't use /\r\n/g, because you can't expect that every line break consists of a carriage return followed by a line feed.

    I'd suggest changing the first few lines to the following, which will solve both your problems:
    Code:
    var noBreaks = document.getElementById("oldText").value;
    noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    
    noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    noBreaks = noBreaks.replace(/(\[-LB-\]){2,}/g, "</p><p>");

  • Users who have thanked venegal for this post:

    four0four (05-09-2009)

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    Ok, I see now what you mean. But you can't use /\r\n/g, because you can't expect that every line break consists of a carriage return followed by a line feed.

    I'd suggest changing the first few lines to the following, which will solve both your problems:
    Code:
    var noBreaks = document.getElementById("oldText").value;
    noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    
    noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    noBreaks = noBreaks.replace(/(\[-LB-\]){2,}/g, "</p><p>");
    Thanks! That solved the problem.

    Although, now it adds empty <p></p> tags if there's a single line break before or after.

    For example:

    [*single line break*]
    plaintext paragraph1
    [*single line break*]
    plaintext paragraph2
    [*single line break*]

    will output the following:

    <p></p>
    <p>plaintext paragraph1</p>
    [*single line break*]
    <p>plaintext paragraph2</p>
    <p></p>

    Here's what I have so far:

    Code:
    function convertText(){
    
    var noBreaks = document.getElementById("oldText").value;
    
    noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    
    noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    
    noBreaks = noBreaks.replace(/(\[-LB-\]){2,}/g, "</p><p>");
    
    re4 = /\[-LB-\]\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re4,"</p><p>");
    
    re5 = /\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re5," ");
    
    noBreaks ='<p>'+noBreaks+'</p>';
    
    noBreaks = noBreaks.replace(/<\/p><p>/g,"</p>\r\n\r\n<p>");
    
    noBreaks = noBreaks.replace(/ +/g, " ");
    
    noBreaks = noBreaks.replace(/<p> *(.*?) *<\/p>/g, "<p>$1</p>");
    
    document.getElementById("newCode").value = noBreaks;
    
    }
    Am I doing something wrong?

    Thanks again! I really appreciate your help.
    Last edited by four0four; 05-08-2009 at 03:24 AM.

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I see. This one will work then:
    Code:
    function convertText(){
    	var noBreaks = document.getElementById("oldText").value;
    	
    	noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    	noBreaks = noBreaks.replace(/(\[-LB-\]){2,}/g, "</p><p>");
    	
    	re5 = /\[-LB-\]/gi;
    	noBreaks = noBreaks.replace(re5," ");
    	
    	noBreaks ='<p>'+noBreaks+'</p>';
    	noBreaks = noBreaks.replace(/<\/p><p>/g,"</p>\r\n\r\n<p>");
    	noBreaks = noBreaks.replace(/ +/g, " ");
    	noBreaks = noBreaks.replace(/<p> *(.*?) *<\/p>/g, "<p>$1</p>");
    	
    	document.getElementById("newCode").value = noBreaks;
    }
    I removed the
    Code:
    re4 = /\[-LB-\]\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re4,"</p><p>");
    because that's already done by code I gave you before and added
    Code:
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    to remove line breaks at the beginning and the end.

  • Users who have thanked venegal for this post:

    four0four (05-09-2009)

  • #7
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    I see. This one will work then:
    Code:
    function convertText(){
    	var noBreaks = document.getElementById("oldText").value;
    	
    	noBreaks = noBreaks.replace(/\r|\n/g,"[-LB-]");
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    	noBreaks = noBreaks.replace(/(\[-LB-\]){2,}/g, "</p><p>");
    	
    	re5 = /\[-LB-\]/gi;
    	noBreaks = noBreaks.replace(re5," ");
    	
    	noBreaks ='<p>'+noBreaks+'</p>';
    	noBreaks = noBreaks.replace(/<\/p><p>/g,"</p>\r\n\r\n<p>");
    	noBreaks = noBreaks.replace(/ +/g, " ");
    	noBreaks = noBreaks.replace(/<p> *(.*?) *<\/p>/g, "<p>$1</p>");
    	
    	document.getElementById("newCode").value = noBreaks;
    }
    I removed the
    Code:
    re4 = /\[-LB-\]\[-LB-\]/gi;
    noBreaks = noBreaks.replace(re4,"</p><p>");
    because that's already done by code I gave you before and added
    Code:
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    to remove line breaks at the beginning and the end.
    Excellent! Thank you so much!

    One last question...it creates empty <p></p> tags if one of these line breaks contain a space or multiple spaces. Can that be fixed?

    For example:

    [*single line break*][space]
    plaintext paragraph1
    [*single line break*]
    plaintext paragraph2
    [*single line break*][space]

    will output the following:

    <p></p>
    <p>plaintext paragraph1</p>
    [*single line break*]
    <p>plaintext paragraph2</p>
    <p></p>


    Many thanks!

  • #8
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Ah, I see. Just another order necessary there. Change
    Code:
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    to
    Code:
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    I think we got it now.

  • Users who have thanked venegal for this post:

    four0four (05-09-2009)

  • #9
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    Ah, I see. Just another order necessary there. Change
    Code:
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    to
    Code:
    	noBreaks = noBreaks.replace(/\s*(\[-LB-\])/g, "$1");
    	noBreaks = noBreaks.replace(/^(\[-LB-\])*/, "");
    	noBreaks = noBreaks.replace(/(\[-LB-\])*$/, "");
    I think we got it now.
    Everything works great! Thank you very much for your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •