可选文本在括号内的正则表达式
P粉949267121
P粉949267121 2024-02-03 21:45:26
0
1
487

这是我的代码。它用于自定义 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, '').replace(/[^\w,éôëçñï]/g,'');

仅将“text1 text2”视为正确答案。我找不到根据我输入的内容正确计算“text1 text2”和“text2”的方法。

P粉949267121
P粉949267121

全部回复(1)
P粉156415696

尝试将整个输入 /^...$/ 与可选捕获组 (?:...) 进行匹配?对于可能会或可能不会出现在输入中的 text1...

str2.replace(/^((?:\([a-zA-Z0-9]+\)\s+)?[a-zA-Z0-9]+).*$/, )

参见https://regex101.com/r/CulGff/1

>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板