开发者

Parsing whitespace to resemble browser view

I'm parsing through some data using javascript and getting results l开发者_C百科ike that look like this:

" The big\n         brown     dog.     "

Of course, in a browser, this wouldn't look so funky:

The big brown dog

But in a text editor, it would look like this:

  The big
        brown      dog

Any ideas on the cleanest way to parse this, so it looks like it is supposed to as plain text?:

The big brown dog

Thanks!


var string = " The big\n         brown     dog.     ";
string = string.replace(/\s{2,}/g, " ");

This just matches all whitespace (when 2 or more whitespace characters are next to each other) and then replaces it with a single space.

Input (minus the quotes):

"The big\n         brown     dog.     "

Output (minus the quotes):

"The big brown dog. "
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜