te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Change Multi line strings to Single line - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change Multi line strings to Single line - Stack Overflow

programmeradmin2浏览0评论

Hi I have some text in following format,

683101,
682303,
682302,
682315,
683581,
686667,
682008,
683572,
683573,
682313,
686672,
683545 etc....

Each line break'ed into next line with "Enter". I have nearly 2000 lines of text like this. i want o display the above string to a single line like this.

683101, 682303, 682302, 682315, 683581, 686667, 682008, 683572, 683573, 682313, 686672, 683545, 686672, 683545 etc..

I think there is some tweak options in CSS for doing this. Is there a way in JavaScript to do this? Actually it is not a requirement for me, am just curious to know how it is done.

  • Edit:

In My text editor it looks like this,

When i try to run it, this is what i get.


Thats why i want to remove the enter, multiline.......

Hi I have some text in following format,

683101,
682303,
682302,
682315,
683581,
686667,
682008,
683572,
683573,
682313,
686672,
683545 etc....

Each line break'ed into next line with "Enter". I have nearly 2000 lines of text like this. i want o display the above string to a single line like this.

683101, 682303, 682302, 682315, 683581, 686667, 682008, 683572, 683573, 682313, 686672, 683545, 686672, 683545 etc..

I think there is some tweak options in CSS for doing this. Is there a way in JavaScript to do this? Actually it is not a requirement for me, am just curious to know how it is done.

  • Edit:

In My text editor it looks like this,

When i try to run it, this is what i get.


Thats why i want to remove the enter, multiline.......

Share Improve this question edited Aug 31, 2015 at 11:05 locknies asked Aug 31, 2015 at 10:58 locknieslocknies 1,3553 gold badges15 silver badges37 bronze badges 3
  • You need to add \ at the end of each line for multi-line strings – Tushar Commented Aug 31, 2015 at 11:06
  • I know adding slash at the end of the line can solve this. i want to know is there any way to do this programatically or why it is not possible. – locknies Commented Aug 31, 2015 at 11:09
  • Because, Javascript's automatic semicolon insertion adds ; at the end of each line, that is causing error. – Tushar Commented Aug 31, 2015 at 11:10
Add a ment  | 

4 Answers 4

Reset to default 5

You can use Regular expression to remove all the linebreaks and replace them using space.

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

Here, \n will match all the line-breaks, and replace them by space

I have a simple way for this. You can do this without extra code. Just write like this -

var str = "12345,\
234234,\
234324,\
234324,\
234324,\
234234";

now just add a slash

Ok, If you don't want to use the above method then use another plan is -

take inside an array and after that use the join method

var str = [12345,
234234,
234324,
234324,
234324,
234234];


str.join(",");

If we are using ES6, Then we have an elegant way to do this using Backtick -

var str = `12345,
234234,
234324,
234324,
234324,
234234`;

Since your data is already ma separated, you can try to add "[" to the beginning and append " ].toString().replace(/\n/g," ") " to the end of your data to get a single line string like this:

[683101,
682303,
682302,
682315,
683581,
686667,
682008,
683572,
683573,
682313,
686672,
683545].toString().replace(/\\n/g," ")

then you get: "683101,682303,682302,682315,683581,686667,682008,683572,683573,682313,686672,683545"

I hope this helps :)

If all you want is to put those values in one line then, you can set those values as the value of a textarea field. This will allow you to read all those values into a javascript string. Afterward you can apply the regular expression that Tushar suggested.

See the code segment below:

<textarea id="content">
683101,
682303,
682302,
682315,
683581,
686667,
682008,
683572,
683573,
682313,
686672,
683545
</textarea>  

Here is the javascript:

var content = $('#content').val();
var content = content.replace(/\n/g, ' ');
console.log(content);
发布评论

评论列表(0)

  1. 暂无评论