2

i'd like to remove any whitespace surrounding a new line, from

Here is a new line. /n 
New line.

to

Here is a new line./n
New line.

I've found various examples how to remove whitespaces, and how to remove new lines, but not how to replace a ' '+/n with a simple /n. I tried sth like the following but it didn't work:

paragraphs = paragraphs.replace(/(\r\n|\n|\r|' '+\n|' '+\r|\n+' '|\r+' ')/gm,'<br>'); 

UPDATE:

that's how i solved it: paragraphs = paragraphs.replace(/\r\n|\n|\r/gm,'\n'); // clears array of empty elements (double new lines) paragraphs = $.grep(paragraphs,function(n){ return(n) }); // clears text of all double whitespaces for (p=0;p

7
  • 1
    There is nothing about jQuery in your question. PS: \ and / chars are not interchangeable
    – zerkms
    Oct 30, 2013 at 23:14
  • alright, "\ " was the missing link. I now built this to get the desired result. thanks for the quick comments! paragraphs = paragraphs.replace(/\ +\n+\ |\n+\ |\ +\n/gm,'\n'); it first takes out the space-newLine-space combination and then the newLine-space and last the space-newLine combinations. I guess I'll have to repeat it to work with all formats, resulting in this unsexy command: paragraphs = paragraphs.replace(/\ +\n+\ |\n+\ |\ +\n|\ +\r+\ |\r+\ |\ +\r|\r\n|\r/gm,'\n');
    – Beezle-Bug
    Oct 31, 2013 at 0:26
  • I don't know what you're calling the missing link, but I think your problem is you added simples quotes. Also, \ doesn t make sense. I just edited my answer to make you a simplier regex proposal
    – Asenar
    Oct 31, 2013 at 0:33
  • you are right, i meant '/ '. i just didn't know the character for whitespace.
    – Beezle-Bug
    Oct 31, 2013 at 1:11
  • 1
    I think if you've got your answer from the answers below then you should mark it as accepted. Or if you have a solution by yourself then we would really appreciate you to post that. Oct 31, 2013 at 2:27

6 Answers 6

2

I don't see the problem, does the following not work ?

var paragraph = "my paragraph with a space at the end \nand a new line";
paragraph.replace(/ \n/, "\n");

// or with multiple spaces, this is working too:
paragraph = "my paragraph with lot of spaces    \nzzzzz".replace(/ +\n/, "\n");

// or, for the more complete regex you want 
paragraphs = paragraphs.replace(/ [ \r\n]+/gm, "\n");
0
0

May be you could split the string by \n and apply a jQuery.trim()

0
paragraphs = paragraphs.replace(/\ +\n/g,'\n');
0

Try this :

HTML :

<div id="myDiv">
    Here is a new line.   /n
    New line.
</div>

javaScript :

//get the innerHTML
var paragraph = document.getElementById("myDiv").innerHTML;
//extract the part that you want to replace using RegExp.exec() method
var newPart = /[ ]*\/n/.exec(paragraph);
//simply replace the new part with what you want
paragraph = paragraph.replace(newPart, '/n');
alert(paragraph);

Demo

2
  • thanks. your idea looks neat - what did i do wrong here: var paragraphs = "here is a new line \n new line"; var newPart = /[ ]*\/n/.exec(paragraphs); paragraphs = paragraphs.replace(newPart, '/n'); $('#input').html(paragraphs);
    – Beezle-Bug
    Oct 31, 2013 at 0:37
  • If you are using jQuery, here's nothing wrong. What "#input" actually is? Oct 31, 2013 at 0:44
0

RegExp you've used, /(\r\n|\n|\r|' '+\n|' '+\r|\n+' '|\r+' ')/gm is not doing what you've intended; the '+ part matches 1+ ' characters; to match single space character just type space ( without apostophes ) you should have put it like this ( correct me if I'm wrong ) : /\r\n|\s+\n|\s+\r|\n+|\r+/g and use it like this: p.replace(/\r\n|\s+\n|\s+\r|\n+|\r+/g,"\n<br>"), or try this function:

//
var ws_nls_ws_2_nl =
( function ( sc, ws_nls_ws_rgx, str_trim_rgx, nl ) {
  return function ( input_txt ) {
    // split on "\n+" surounded by blanks
    // join with "\n"
    // trim the result string
    return sc( input_txt ).split( ws_nls_ws_rgx ).join( nl ).replace( str_trim_rgx, "" );
  };
} )(

  // get global String ctor
  ( function () { return String; } )(),

  // new line(s) with blanks around it, global
  /[\s\uFEFF\xA0]+(?:\n+|(?:\r\n)+|\r+)[\s\uFEFF\xA0]+/g,

  // str-trim reg, 
  // blanks at start or end
  /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,

  "\n"

);
//
//  ws_nls_ws_2_nl("        Here is a new line.  \n   New line.   ");
//
//
0

alright, that's how i cleaned my input textfield eventually:

function cleanText(){
//aligns all new lines and splits text into single paragraphs
paragraphs = input.replace(/\r\n|\n|\r/gm,'\n');
paragraphs = paragraphs.split('\n');
// clears array of empty elements (double new lines)
paragraphs = $.grep(paragraphs,function(n){ return(n) });
// clears text of all double whitespaces
for (p=0;p<paragraphs.length; p++){
    paragraphs[p] = paragraphs[p].replace(/\s+/g, ' ');
    paragraphs[p] = $.trim(paragraphs[p]);
}
// joins everything to one string
var cleanedText = paragraphs.join("\n ") ;
// gets an array of single words
wordsArray=cleanedText.split(' ');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.