可選文字在括號內的正規表示式
P粉949267121
P粉949267121 2024-02-03 21:45:26
0
1
459

這是我的程式碼。它用於自訂 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

#>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板