我希望我的用户脚本在页面上用" B", 替换单词" A",除非 字" C"存在。
如果" C"存在,然后我想替换" A"与" D"代替。
例如,
Cat
替换为Feline
。Kitten
,则会将Cat
更改为Meow
。我找到了this old userscript written by Joe Simmons;请参阅下面的相关代码。虽然效果非常好,但如何添加条件功能?
(function () { 'use strict';
var words = {
// Syntax: 'Search word' : 'Replace word',
'your a': 'you\'re a',
'imo': 'in my opinion',
'im\\*o': 'matching an asterisk, not a wildcard',
'/\\bD\\b/g': '[D]',
///////////////////////////////////////////////////////
'': ''
};
var regexs = [],
replacements = [],
tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
rIsRegexp = /^\/(.+)\/([gim]+)?$/,
word, text, texts, i, userRegexp;
// prepareRegex by JoeSimmons
// used to take a string and ready it for use in new RegExp()
function prepareRegex (string) {
return string.replace (/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
}
// function to decide whether a parent tag will have its text replaced or not
function isTagOk (tag) {
return tagsWhitelist.indexOf (tag) === -1;
}
delete words['']; // so the user can add each entry ending with a comma,
// I put an extra empty key/value pair in the object.
// so we need to remove it before continuing
// convert the 'words' JSON object to an Array
for (word in words) {
if (typeof word === 'string' && words.hasOwnProperty (word) ) {
userRegexp = word.match (rIsRegexp);
// add the search/needle/query
if (userRegexp) {
regexs.push (
new RegExp (userRegexp[1], 'g')
);
}
else {
regexs.push (
new RegExp (prepareRegex (word)
.replace (/\\?\*/g, function (fullMatch) {
return fullMatch === '\\*' ? '*' : '[^ ]*';
} ),
'g'
)
);
}
// add the replacement
replacements.push(words[word]);
}
}
// do the replacement
texts = document.evaluate ('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
for (i = 0; text = texts.snapshotItem (i); i += 1) {
if (isTagOk (text.parentNode.tagName) ) {
regexs.forEach (function (value, index) {
text.data = text.data.replace (value, replacements[index]);
} );
}
}
} () );
答案 0 :(得分:0)
您可以通过创建在猫过滤器之前触发的小猫过滤器来滥用执行顺序。
require(data.table)
num_rows <- 5
dt <- data.table(column1 = character(num_rows),
column2 = integer(num_rows),
column3 = date(num_rows)
)
dt <- data.table(column1 = character(num_rows),
column2 = integer(num_rows),
column3 = POSIXct(num_rows)
)
dt <- data.table(column1 = character(num_rows),
column2 = integer(num_rows),
column3 = IDateTime(num_rows)
)
它实际上不如实施回调那么烦人。