【问题标题】:greasemonkey userscript injects variables within another variable油脂猴用户脚本在另一个变量中注入变量
【发布时间】:2012-03-31 14:06:08
【问题描述】:

所以目前我正在尝试向我制作的用户脚本添加一个功能。

目前用户脚本会将一些文本放入变量中。现在还有 2 个 var,目前用于将文本添加到第一个 var 中文本的开头和结尾。这是一个代码示例,可以让您更好地了解它当前在做什么

elmTextarea.value   = opening + elmTextarea.value + closing;

其中 elmTextarea 是用户脚本获取的字符串,而开头和结尾是我放在开头和结尾的内容。

现在,我想要发生的是,如果变量 elmTextarea 包含任何 [quote*]blahblahblah[/quote] 它基本上会排除这些区域(不是星号可以是任何东西,它是格式

[quote='name' pid='20784507' dateline='1331755619'])

但也可能只是 [quote]

所以这里是一个简单的例子,你可以更好地理解

如果 elmTextarea 是

blahbla
[quote='name' pid='20784507' dateline='1331755619']
some more text
[/quote]

here is some more

[quote='name' pid='20523454507' dateline='1335435619']
some more text in here
[/quote]

and finally this text

会变成

opening + blahbla + closing
[quote='name' pid='20784507' dateline='1331755619']
some more text
[/quote]

opening + here is some more + closing

[quote='name' pid='20523454507' dateline='1335435619']
some more text in here
[/quote]

opening + and finally this text + closing

所以我对这将如何工作有了一个想法,这是我尝试的实现

var openingquote = closing + "[quote";
var closingquote = "[/quote]" + opening;
elmTextarea.value   = opening + elmTextarea.value + closing;
elmTextarea.value = elmTextarea.value.replace(/[quote/gim, openingquote);
elmTextarea.value = elmTextarea.value.replace(/[\/quote]/gim, closingquote);

但是将这些行添加到我的代码中会使我的整个脚本无法工作。关于为什么这不起作用以及如何解决它的任何想法。

【问题讨论】:

    标签: javascript variables split greasemonkey quotes


    【解决方案1】:

    方括号在正则表达式中具有特殊含义。这些也应该被转义:

    elmTextarea.value = elmTextarea.value.replace(/\[quote/gim, openingquote);
    elmTextarea.value = elmTextarea.value.replace(/\[\/quote]/gim, closingquote);
    //                                             ^ Escape [ using \[
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多