简体   繁体   中英

greasemonkey userscript injects variables within another variable

So currently I am trying to add a feature to a userscript I have made.

Currently the userscript will get some text into a variable. Now there are also have 2 vars, that currently are for adding text to the begenning and end of the text in the first var. Here is a code sample to make you better understand what it is currently doing

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

Where elmTextarea is the string that the userscript got and opening and closing are the things I put in the beginning and end of it.

Now, what I want to happen is that if the variable elmTextarea includes any [quote*]blahblahblah[/quote] it essentially will exclude those areas(not the star could be anything, it is in the format

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

but could also be just [quote]

So here is a quick example so you can better understand

if elmTextarea is

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

it would become

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

So I have an idea as to how this would work, and here is my attempted implementation

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);

but adding these lines into my code makes my whole script not work. Any ideas as to why this does not work and how to fix it.

Square brackets have a special meaning in a Regular expression. These should also be escaped:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM