这是我的代码。用于自定义anki中的输入框:
function main() {
//Script for modifying 'Show Answer' behavior for Input types.
var htmlTextNodes = [];
var innerHTMLText = [];
if (!document.getElementById('typeans')) {
return;
}
var htmlNodeLength =document.getElementById('typeans').childNodes.length;
var typedAnswer;
var correctAnswer;
var firstBr = null;
var secondBr;
//capture each node to array
for (i = 0; i < htmlNodeLength; i++) {
htmlTextNodes[i] = document.getElementById('typeans').childNodes[i];
innerHTMLText[i] = document.getElementById('typeans').childNodes[i].innerHTML;
//locate <br> tags for output change markers
if (document.getElementById('typeans').childNodes[i].nodeName == "BR") {
console.log("Runs if BR");
if (firstBr != null) {
secondBr = i;
} else {
firstBr = i;
};
};
};
//If answer is correct, firstBr will still be null, so must set to length of typeans.childNode
if (firstBr == null) {
firstBr = htmlNodeLength;
};
//assemble typed and correct answer strings
str2 = innerHTMLText.slice(0,firstBr).join("");
var typeParse = str2.replace(/[^\w,éôëçñï]/g,' ');
var typedAnswer = typeParse.replace(/\s/g, '');
var typedAnsDis = str2.replace(/[^a-zA-Z0-9,éôëçñï]/g,' ');
var typedUpper = typedAnswer.toUpperCase();
//typedUpper = "KITTEN"
var corr = document.getElementById('correctAnswer');
var str2 = corr.innerHTML;
var correctParse = str2.replace(/[^\w,éôëçñï]/g,'');
var correctAnswer = correctParse.replace(/\s/g, '');
//split alternative answers into array
var getalt = document.getElementById('altmean');
var altmean = getalt.innerHTML.replace(/(\([^)]*\))/g, '').replace(/[^\w,éôëçñï]/g,' ');
altmean += ", ";
altmean += correctAnswer;
var array = altmean.split(",").map(function(item) {
return item.trim();
});
if (!array.includes(correctAnswer)) {
array.push(correctAnswer);}
for ( var i = 0; i < array.length; i++ ) {
array[i] = "" + array[i].toUpperCase() + "";
array[i] = "" + array[i].replace(/[/\W, " "]/g,' ');; + "";
array[i] = "" + array[i].replace(/\s/g, ''); + "";
}
//Modify answer output
if ((array.indexOf(typedUpper) > -1) && (!(typedUpper == "" ))) {
var c = "<div class=\"animated zoomIn\" id='correct'>"+typedAnsDis+"</div>";
var d = document.getElementById('typeans');
d.innerHTML = c;
} else {
if(typedAnsDis == "") {
var g = "<div class=\"animated shake\" id='empty'>{{Meaning}}"+typedAnsDis+"</div>";
var h = document.getElementById('typeans');
h.innerHTML = g;
const div = document.getElementById('empty');
div.innerHTML = div.textContent.split('').filter(char => /^[A-Za-z\s\.,;!?"'-~-éū]+$/.test(char) && !/[^\x00-\x7F]/.test(char)).join('');
} else {
var e = "<div class=\"animated flash\" id='incorrect'>"+typedAnsDis+"</div>";
var f = document.getElementById('typeans');
f.innerHTML = e;
}
}
};
main();
假设我有一个文本,如“(text1)text2”。我想找到一种方法,使脚本将“text1 text2”和“text2”都视为正确答案。
直到现在我才发现
var correctParse = str2.replace(/(\([^)]*\))/g, '').replace(/[^\w,éôëçñï]/g,'');
仅将“text2”视为正确答案,而
var correctParse = str2.replace(/(\([^)]*\))/g, '$1').replace(/[^\w,éôëçñï]/g,'$1');
仅将“text1 text2”视为正确答案。我找不到一种方法来计算“text1 text2”和“text2”正确取决于我键入的内容。
1条答案
按热度按时间eh57zj3b1#
尝试将整个输入/^...$/与可选的捕获组(?:...)?对于可能会或可能不会出现在输入中的text 1...
参见https://regex101.com/r/CulGff/1