使用 JavaScript 删除这些换行符

发布于 2024-11-08 22:32:31 字数 532 浏览 0 评论 0原文

我一生都不知道如何删除这种奇怪的格式。这是 JSON.stringify(someObj) 之后发送给我的内容: http://jsbin.com/ohoto5/2/edit

单击预览,您将看到格式。我需要将其设置为一行,以便它可以进入 JSONP 响应(即 jsonp12345(...) )。如果它是多行,则会触发语法错误。我已经尝试了我能想到的所有正则表达式组合,但没有任何方法可以删除这些行。甚至没有 /[\r\n\s\t]/gi 。唯一似乎删除它的是: /[\w\n]/gi 但是,问题是我丢失了所有单词!

有什么帮助吗?请不要修复 jQuery...我需要纯 JavaScript。

这是 Chrome 中的对象(图片): 在此处输入图像描述

For the life of me I can't figure out how to remove this weird formatting. This is what is getting sent to me after a JSON.stringify(someObj): http://jsbin.com/ohoto5/2/edit

Click preview and you'll see the formatting. I need that to be one line so that it can go in a JSONP response ( i.e. jsonp12345(...) ). If it's multiple lines it will trigger a syntax error. I've tried every regex combo i can think of and nothing removes the lines. No /[\r\n\s\t]/gi even. The only thing that seems to remove it is: /[\w\n]/gi however, the issue is that i lose all the words!

Any help? Please no jQuery fixes... I'm in need of pure JavaScript.

Here is the object in Chrome (pic):
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人问我粥可暖 2024-11-15 22:32:31

在我看来,这应该可以做到:

s.replace(/[\r\n]/g, '')

jsFiddle 演示

似乎所有不必要的换行符都被删除了。

Looks to me that this should do it:

s.replace(/[\r\n]/g, '')

jsFiddle Demo

Seems like every unnecessary line-break is removed.

夜灵血窟げ 2024-11-15 22:32:31

很难说出需要什么,但是浏览器识别为空白的内容之间存在很多差异,请考虑精确设置您希望匹配的字符,例如

// List of characters matched as white space 
var whiteSpace = '\u0009\u000a\u000b\u000c\u000d\u0020\u00a0' + 
                 '\u1680\u180e\u2000\u2001\u2002\u2003\u2004' + 
                 '\u2005\u2006\u2007\u2008\u2009\u200a\u200b' + 
                 '\u2028\u2029\u202f\u205f\u3000'; 
var re = new RegExp('[' + whiteSpace + ']+', 'g');

var x = 'lots and  lots     of    space     ';

alert(x.replace(re, ' ').replace(/ +/,' '));

Difficult to tell what is needed, however there are many differences between what browsers recognise as whitespace, consider setting precisely what characters you wish to match, e.g.

// List of characters matched as white space 
var whiteSpace = '\u0009\u000a\u000b\u000c\u000d\u0020\u00a0' + 
                 '\u1680\u180e\u2000\u2001\u2002\u2003\u2004' + 
                 '\u2005\u2006\u2007\u2008\u2009\u200a\u200b' + 
                 '\u2028\u2029\u202f\u205f\u3000'; 
var re = new RegExp('[' + whiteSpace + ']+', 'g');

var x = 'lots and  lots     of    space     ';

alert(x.replace(re, ' ').replace(/ +/,' '));
苦妄 2024-11-15 22:32:31

放弃 JS 正则表达式后,我尝试了 PHP,并得到了第一次尝试:

preg_replace('/\s\s+/', ' ', $referrer['cache'])

其中缓存是上面我原来的 JSBin 链接中的 JSON。现在它返回:

callback([{"LatLng":{"Ba":45.531124,"Ca":-122.68374699999998},"InfoWindow":" <address>1125 NW 12th Ave, Portland, OR</address> <p>My first apartment</p> ","originalAddress":"1125 NW 12th Ave, Portland, OR"},{"LatLng":{"Ba":45.5138621,"Ca":-122.67767300000003},"InfoWindow":" <address>1330 SW 3rd Ave, Portland, OR</address> <p>My 2nd apartment</p> ","originalAddress":"1330 SW 3rd Ave, Portland, OR"},{"LatLng":{"Ba":45.748955,"Ca":-122.47959000000003},"InfoWindow":" <address>17501 NE 188th Ct, Brush Prairie, WA</address> <p>The first place I lived by my own</p> ","originalAddress":"17501 NE 188th Ct, Brush Prairie, WA"},{"LatLng":{"Ba":45.756944,"Ca":-122.43575800000002},"InfoWindow":" <address>18607 NE Erickson Rd, Brush Prairie, WA</address> <p>Last place I lived with my parents</p> ","originalAddress":"18607 NE Erickson Rd, Brush Prairie, WA"}])

有趣的是,JS 中完全相同的正则表达式并没有给出相同的结果 -_-

After giving up on JS regex I tried PHP and got it first try:

preg_replace('/\s\s+/', ' ', $referrer['cache'])

where the cache is the JSON in my original JSBin link above. Now it returns:

callback([{"LatLng":{"Ba":45.531124,"Ca":-122.68374699999998},"InfoWindow":" <address>1125 NW 12th Ave, Portland, OR</address> <p>My first apartment</p> ","originalAddress":"1125 NW 12th Ave, Portland, OR"},{"LatLng":{"Ba":45.5138621,"Ca":-122.67767300000003},"InfoWindow":" <address>1330 SW 3rd Ave, Portland, OR</address> <p>My 2nd apartment</p> ","originalAddress":"1330 SW 3rd Ave, Portland, OR"},{"LatLng":{"Ba":45.748955,"Ca":-122.47959000000003},"InfoWindow":" <address>17501 NE 188th Ct, Brush Prairie, WA</address> <p>The first place I lived by my own</p> ","originalAddress":"17501 NE 188th Ct, Brush Prairie, WA"},{"LatLng":{"Ba":45.756944,"Ca":-122.43575800000002},"InfoWindow":" <address>18607 NE Erickson Rd, Brush Prairie, WA</address> <p>Last place I lived with my parents</p> ","originalAddress":"18607 NE Erickson Rd, Brush Prairie, WA"}])

Funny how the exact same regex in JS doesn't give the same result -_-

复古式 2024-11-15 22:32:31

CRLF 被编码为 \r\n,因此

str = str.replace(/(\r\n)/g, '');

应该可以完成这项工作。

CRLF is encoded as \r\n, so

str = str.replace(/(\r\n)/g, '');

should do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文