最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

replace whitespace AND newline in javascript - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Nov 4, 2013 at 22:51 Beezle-Bug asked Oct 30, 2013 at 23:13 Beezle-BugBeezle-Bug 1676 silver badges13 bronze badges 7
  • 1 There is nothing about jQuery in your question. PS: \ and / chars are not interchangeable – zerkms Commented Oct 30, 2013 at 23:14
  • alright, "\ " was the missing link. I now built this to get the desired result. thanks for the quick ments! paragraphs = paragraphs.replace(/\ +\n+\ |\n+\ |\ +\n/gm,'\n'); it first takes out the space-newLine-space bination and then the newLine-space and last the space-newLine binations. I guess I'll have to repeat it to work with all formats, resulting in this unsexy mand: paragraphs = paragraphs.replace(/\ +\n+\ |\n+\ |\ +\n|\ +\r+\ |\r+\ |\ +\r|\r\n|\r/gm,'\n'); – Beezle-Bug Commented 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 Commented Oct 31, 2013 at 0:33
  • you are right, i meant '/ '. i just didn't know the character for whitespace. – Beezle-Bug Commented 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. – Md Ashaduzzaman Commented Oct 31, 2013 at 2:27
 |  Show 2 more ments

6 Answers 6

Reset to default 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 plete regex you want 
paragraphs = paragraphs.replace(/ [ \r\n]+/gm, "\n");

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

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

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

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.   ");
//
//

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(' ');
}
发布评论

评论列表(0)

  1. 暂无评论