将 textareas 字符串值转换为以换行符分隔的 JavaScript 数组

发布于 2024-12-20 14:56:04 字数 937 浏览 2 评论 0原文

我有一个 textarea,用户最多可以在其中写入 1000 个字符。我需要获取 jQuery('#textarea').val() 并创建一个数组,其中每个项目都是 textarea 值的一行。这意味着:

这是文本区域内的一行漂亮的线。
这是另一行。
(我们假设这一行是空的 - 它应该被忽略)。
有人在上面留下了超过 2 行新行。

应该转换为 JavaScript 数组:

var texts = [];
text[0] = 'This is a nice line inside the textarea.';
text[1] = 'This is another line.';
text[2] = 'Someone left more than 2 new lines above.';

这样它们就可以轻松 内爆 为查询字符串(这是提供商要求的 qs 格式):

example.com/process.php?q=["This is a nice line inside the textarea.","This is another line.","Someone left more than 2 new lines above."]

我尝试了 phpjs explode()< /a> 和string.split("\n") 方法,但它们不处理额外的新行(也称为换行符)。有什么想法吗?

I have a textarea where the user can write up to 1000 characters. I need to get the jQuery('#textarea').val() and create an array where each item is a line of the textarea's value. That means:

This is a nice line inside the textarea.
This is another line.
(let's asume this line is empty - it should be ignored).
Someone left more than 2 new lines above.

Should be converted to a JavaScript array:

var texts = [];
text[0] = 'This is a nice line inside the textarea.';
text[1] = 'This is another line.';
text[2] = 'Someone left more than 2 new lines above.';

That way they can be easily imploded for to querystring (this is the qs format required by the provider):

example.com/process.php?q=["This is a nice line inside the textarea.","This is another line.","Someone left more than 2 new lines above."]

I tried both the phpjs explode() and string.split("\n") approaches but they doesn't take care of the extra new lines (aka line breakes). Any ideas?

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

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

发布评论

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

评论(4

笑红尘 2024-12-27 14:56:04

String.prototype.split() 很不错。

var lines = $('#mytextarea').val().split(/\n/);
var texts = [];
for (var i=0; i < lines.length; i++) {
  // only push this line if it contains a non whitespace character.
  if (/\S/.test(lines[i])) {
    texts.push($.trim(lines[i]));
  }
}

请注意,并非所有平台都支持 String.prototype.split,因此 jQuery 提供了 $.split()。它只是修剪字符串末端周围的空白。

$.trim(" asd  \n") // "asd"

在这里查看:http://jsfiddle.net/p9krF/1/

String.prototype.split() is sweet.

var lines = $('#mytextarea').val().split(/\n/);
var texts = [];
for (var i=0; i < lines.length; i++) {
  // only push this line if it contains a non whitespace character.
  if (/\S/.test(lines[i])) {
    texts.push($.trim(lines[i]));
  }
}

Note that String.prototype.split is not supported on all platforms, so jQuery provides $.split() instead. It simply trims whitespace around the ends of a string.

$.trim(" asd  \n") // "asd"

Check it out here: http://jsfiddle.net/p9krF/1/

铁憨憨 2024-12-27 14:56:04

使用 split 函数:

var arrayOfLines = $("#input").val().split("\n");

Use split function:

var arrayOfLines = $("#input").val().split("\n");
莫多说 2024-12-27 14:56:04
var split = $('#textarea').val().split('\n');
var lines = [];
for (var i = 0; i < split.length; i++)
    if (split[i]) lines.push(split[i]);
return lines;
var split = $('#textarea').val().split('\n');
var lines = [];
for (var i = 0; i < split.length; i++)
    if (split[i]) lines.push(split[i]);
return lines;
赠我空喜 2024-12-27 14:56:04

试试这个

var lines = [];
$.each($('textarea').val().split(/\n/), function(i, line){
   if(line && line.length){
      lines.push(line);
   }
});

Try this

var lines = [];
$.each($('textarea').val().split(/\n/), function(i, line){
   if(line && line.length){
      lines.push(line);
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文