-1

我正在开发一个简单的项目,该项目会在您可以在聊天时使用的图像中生成 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>

有两个<textarea>s。第一个用字符串填充,第二个用它们的替换值替换字符串。

并且该功能在 keyup 事件后开始工作。它应该可以完美地工作,但它会返回一些从字符到 g 的奇怪替换(其余的都在工作)。那么有解决办法吗?或者另一种方式,比如使用数组替换?

4

1 回答 1

0

您不需要更换 100 个,只需一个即可。

insertedText.replace(/([a-z])/gi, '[[f9.ch$1]]')

http://jsfiddle.net/PRYWm/1/

于 2012-07-27T23:12:43.490 回答