简体   繁体   English

使用JavaScript删除这些换行符

[英]Remove These Line Breaks with JavaScript

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 这是在JSON.stringify(someObj)之后发送给我的JSON.stringify(someObj)http : JSON.stringify(someObj)

Click preview and you'll see the formatting. 单击预览,您将看到格式。 I need that to be one line so that it can go in a JSONP response ( ie jsonp12345(...) ). 我需要成为一行,以便它可以进入JSONP响应(即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. 我已经尝试过我能想到的每一个regex组合,但是没有什么能消除这些障碍。 No /[\\r\\n\\s\\t]/gi even. 甚至没有/[\\r\\n\\s\\t]/gi The only thing that seems to remove it is: /[\\w\\n]/gi however, the issue is that i lose all the words! 似乎唯一将其删除的是: /[\\w\\n]/gi但是问题是我丢失了所有文字!

Any help? 有什么帮助吗? Please no jQuery fixes... I'm in need of pure JavaScript. 请不要使用jQuery修复程序...我需要纯JavaScript。

Here is the object in Chrome (pic): 这是Chrome(图片)中的对象: 在此处输入图片说明

Looks to me that this should do it: 在我看来这应该这样做:

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

jsFiddle Demo jsFiddle演示

Seems like every unnecessary line-break is removed. 似乎每个不必要的换行符都被删除。

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, eg 很难说出所需的内容,但是浏览器将哪些内容识别为空格之间存在许多差异,请考虑准确设置要匹配的字符,例如

// 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(/ +/,' '));

After giving up on JS regex I tried PHP and got it first try: 放弃JS regex之后,我尝试了PHP,并首先尝试了它:

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

where the cache is the JSON in my original JSBin link above. 在我上面的原始JSBin链接中,缓存是JSON。 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 -_- 有趣的是,JS中完全相同的正则表达式不会给出相同的结果-_-

CRLF is encoded as \\r\\n, so CRLF编码为\\ r \\ n,因此

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

should do the job. 应该做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM