私は、チャット中に使用できる画像で Facebook BBCode (またはそのようなもの) を生成する単純なプロジェクトに取り組んでいます。
これが私の完全なコードです:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function gear()
{
var textArea = document.getElementById("id");
var insertedText = theForm.theText.value;
var charChanger = insertedText.replace(/a/ig, "[[f9.cha]] ").replace(/b/gi, "[[f9.chb]] ").replace(/c/gi, "[[f9.chc]] ").replace(/d/gi, "[[f9.chd]] ").replace(/e/gi, "[[f9.che]] ").replace(/f/gi, "[[f9.chf]] ").replace(/g/gi, "[[f9.chg]] ").replace(/h/gi, "[[f9.chh]] ").replace(/i/gi, "[[f9.chi]] ").replace(/j/gi, "[[f9.chj]] ").replace(/k/gi, "[[f9.chk]] ").replace(/l/gi, "[[f9.chl]] ").replace(/m/gi, "[[f9.chm]] ").replace(/n/gi, "[[f9.chn]] ").replace(/o/gi, "[[f9.cho]] ").replace(/p/gi, "[[f9.chp]] ").replace(/q/gi, "[[f9.chq]] ").replace(/r/gi, "[[f9.chr]] ").replace(/s/gi, "[[f9.chs]] ").replace(/t/gi, "[[f9.cht]] ").replace(/u/gi, "[[f9.chu]] ").replace(/v/gi, "[[f9.chv]] ").replace(/w/gi, "[[f9.chw]] ").replace(/x/gi, "[[f9.chx]] ").replace(/y/gi, "[[f9.chy]] ").replace(/z/gi, "[[f9.chz]] ");
textArea.innerHTML = charChanger;
}
</script>
<div align="center"><form name="theForm">
<textarea rows="5" name="theText" cols="120" onkeyup="gear();"></textarea>
<br>
<textarea readonly id="id" rows="20" cols="120"></textarea>
</form></div>
</body>
</html>
2 つ<textarea>
の s があります。最初のものは文字列で満たされ、2 つ目は文字列を置換値で置き換えます。
そして、関数は keyup イベントの後に動作を開始します。完全に機能するはずですが、文字から g までの奇妙な置換を返します (残りは機能しています)。それで、修正はありますか?または、配列を使用して置き換えるなどの別の方法はありますか?