Javascript 将换行符减少到两个?

发布于 2024-12-10 01:41:50 字数 2323 浏览 1 评论 0原文

我正在使用以下 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 技术交流群。

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

发布评论

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

评论(2

凉月流沐 2024-12-17 01:41:50

使用正则表达式。以下代码将用两个换行符替换 2 个以上的换行符。

string = string.replace(/\n{2,}/g, "\n\n")

我建议显示一个标志,通知读者引用已消失。考虑以下几点:

用户A的第50条帖子:我的鱼死了=(
==============================
userB 发布 51 条信息:

<块引用>

[quote=Niceidea]
(2页前) blabla呢? ...
[/引用]

惊人的!

==============================

应用代码后:

==============================
用户 A 发表 50 篇文章:我的鱼死了 =(
==============================
帖子 51 由 userB: 太棒了!

==============================

前面的发帖者可能会被冒犯,因为他不知道回复的上下文。

Use a RegExp. The following code will replace 2+ new lines by two newlines.

string = string.replace(/\n{2,}/g, "\n\n")

I recommend to show a sign that notifies the reader of the fact that a quote has disappeared. Consider the following:

Post 50 by userA: My fish died =(
============================
Post 51 by userB:

[quote=Niceidea]
(2 pages ago) What about blabla. ...
[/quote]

Awesome!

============================

After applying your code:

============================
Post 50 by userA: My fish died =(
============================
Post 51 by userB: Awesome!

============================

The previous poster might be offended, because he didn't know the context of the reply.

丘比特射中我 2024-12-17 01:41:50

我找到了一个很好的解决方案:更少的代码并且正是您想要的东西:

var output,
    theSelection = 'Text.\n\n[quote="User 2"]\nQuote tex\nt.\n[/quote]\n\n\n\n\nMore text.';

    /*
     * theSelection is:
     * 'Text.
     *  
     *  [quote="User 2"]
     *  Quote tex
     *  t.
     *  [/quote]
     *
     *
     *
     *  More text.'
     */

if (theSelection) {
    output = theSelection.replace(/\[quote=".*"\][\s\S]*\[\/quote\]/gi, '')
                         .replace(/\n{2,}/g, '\n\n');
}

console.log(output);

/*
 * Outputs:
 * 'Text.
 *
 * More text.'
 * 
 */

I have figured out a nice solution: less code and exactly the thing you wanted:

var output,
    theSelection = 'Text.\n\n[quote="User 2"]\nQuote tex\nt.\n[/quote]\n\n\n\n\nMore text.';

    /*
     * theSelection is:
     * 'Text.
     *  
     *  [quote="User 2"]
     *  Quote tex
     *  t.
     *  [/quote]
     *
     *
     *
     *  More text.'
     */

if (theSelection) {
    output = theSelection.replace(/\[quote=".*"\][\s\S]*\[\/quote\]/gi, '')
                         .replace(/\n{2,}/g, '\n\n');
}

console.log(output);

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