开发者

Parsing whitespace to resemble browser view

开发者 https://www.devze.com 2023-02-28 17:01 出处:网络
I\'m parsing through some data using javascript and getting results l开发者_C百科ike that look like this:

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

精彩评论

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