Javascript 将换行符减少到两个?
我正在使用以下 javascript:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
... 在将文本发送到回复框之前从文本中删除嵌套引号,但它会留下换行符来代替清除的嵌套引号。因此,举例来说,如果我引用用户 1 的一篇帖子,内容是...
Text.
[quote="User 2"]Quote text.[/quote]
More text.
它在回复框中显示为...
[quote="User 1"]Text.
More text.[/quote]
我将在该 javascript 中添加什么来删除此空格或将新行之间的空格限制为仅两个换行符,以便回复框中显示的最终文本显示为:
[quote="User 1"]Text.
More text.[/quote]
感谢您的帮助。
编辑:
这是代码的较大部分:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
if (theSelection)
{
if (bbcodeEnabled)
{
insert_text('[quote="' + username + '"]' + '\n' + '\n' + theSelection + '[/quote]' + '\n' + '\n');
}
else
{
insert_text(username + ' ' + l_wrote + ':' + '\n');
var lines = split_lines(theSelection);
for (i = 0; i < lines.length; i++)
{
insert_text('> ' + lines[i] + '\n');
}
}
}
我将...
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
...放在两个现有的“if (theSelection)”语句之间,并且效果很好。不过还有一个问题。嵌套引用删除发生在引用文本放置在其自己的引用标签内之前,并且在开始引用标签之后添加两个换行符。因此,如果引用的帖子在任何原始文本之前包含嵌套引用,则上述代码生成的文本在开头处有四个换行符,而不是预期的两个换行符。所以我想我需要放置...
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
... after theSelection 插入到它自己的引号标签中,此时“if (theSelection)”似乎不再起作用,我对 javascript 如此陌生,我不知道如何用反映先前“if (theSelection)”语句输出的内容替换“theSelection”。我希望这至少有一点道理。
I'm using the following javascript:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
... to remove nested quotes from text before it is sent to a reply box, but it leaves behind line breaks in place of the purged nested quotes. So, for instance, if I quote a post from User 1 which says...
Text.
[quote="User 2"]Quote text.[/quote]
More text.
It shows up in the reply box as...
[quote="User 1"]Text.
More text.[/quote]
What would I add to this javascript to either remove this space or limit the space between new lines to just two line breaks, so that the final text displayed in the reply box appears as:
[quote="User 1"]Text.
More text.[/quote]
Thanks for any help.
EDIT:
Here's a larger bit of the code:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
if (theSelection)
{
if (bbcodeEnabled)
{
insert_text('[quote="' + username + '"]' + '\n' + '\n' + theSelection + '[/quote]' + '\n' + '\n');
}
else
{
insert_text(username + ' ' + l_wrote + ':' + '\n');
var lines = split_lines(theSelection);
for (i = 0; i < lines.length; i++)
{
insert_text('> ' + lines[i] + '\n');
}
}
}
I placed...
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
... between the two existing "if (theSelection)" statements, and it worked great. One remaining issue though. The nested quote removal happens before the quoted text is placed within its own quote tags, and two line breaks are added after the opening quote tag. So if a quoted post contained a nested quote before any original text, then the text that the above code generates has four line breaks at the opening instead of the intended two. So I think I would need to place...
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
... after theSelection is inserted within its own quote tags, at which point "if (theSelection)" seems to no longer work, and I'm so new to javascript that I don't know how to replace "theSelection" with something that will reflect the output of the previous "if (theSelection)" statement. I hope that made at least a little sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用正则表达式。以下代码将用两个换行符替换 2 个以上的换行符。
我建议显示一个标志,通知读者引用已消失。考虑以下几点:
应用代码后:
前面的发帖者可能会被冒犯,因为他不知道回复的上下文。
Use a RegExp. The following code will replace 2+ new lines by two newlines.
I recommend to show a sign that notifies the reader of the fact that a quote has disappeared. Consider the following:
After applying your code:
The previous poster might be offended, because he didn't know the context of the reply.
我找到了一个很好的解决方案:更少的代码并且正是您想要的东西:
I have figured out a nice solution: less code and exactly the thing you wanted: