This is my code. It is used to customize the typing box in 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();
Suppose I have a text like "(text1) text2". I want to find a way to have the script consider both "text1 text2" and "text2" as the correct answer.
I didn’t find out until now
var correctParse = str2.replace(/(\([^)]*\))/g, '').replace(/[^\w,éôëçñï]/g,'');
Only consider "text2" as the correct answer
var correctParse = str2.replace(/(\([^)]*\))/g, '').replace(/[^\w,éôëçñï]/g,'');
Only "text1 text2" is considered the correct answer. I can't find a way to correctly calculate "text1 text2" and "text2" based on my input.
Try to match the entire input /^...$/ with an optional capturing group (?:...)? For text1...
which may or may not appear in the inputSeehttps://regex101.com/r/CulGff/1
>