Javascript表格缩短 - UTF-8转换编码

时间:2014-10-08 13:41:23

标签: javascript loops encoding

我有一个脚本,我们希望以某种方式使它变小,而不是重复它这么多次。无论如何都要使这段代码更短,所以我们不必再创建10次重复集。

在文本字段中,我们有类似的内容:

onKeyUp="replaceWordChars(this.value)
onKeyUp="replaceWordChars2(this.value)
onKeyUp="replaceWordChars3(this.value)

<script>
function replaceWordChars(text) {
var s = text;
s = s.replace(/[\u2018|\u2019|\u201A]/g, "\'");
s = s.replace(/[\u201C|\u201D|\u201E]/g, "\"");
s = s.replace(/\u2026/g, "...");
s = s.replace(/[\u2013|\u2014]/g, "-");
s = s.replace(/\u02C6/g, "^");
s = s.replace(/\u2039/g, "<");
s = s.replace(/\u203A/g, ">");
s = s.replace(/[\u02DC|\u00A0]/g, " ");
document.getElementById("textcounter1").value = s;}

function replaceWordChars2(text) {
var s = text;
s = s.replace(/[\u2018|\u2019|\u201A]/g, "\'");
s = s.replace(/[\u201C|\u201D|\u201E]/g, "\"");
s = s.replace(/\u2026/g, "...");
s = s.replace(/[\u2013|\u2014]/g, "-");
s = s.replace(/\u02C6/g, "^");
s = s.replace(/\u2039/g, "<");
s = s.replace(/\u203A/g, ">");
s = s.replace(/[\u02DC|\u00A0]/g, " ");
document.getElementById("textcounter2").value = s;}

function replaceWordChars3(text) {
var s = text;
s = s.replace(/[\u2018|\u2019|\u201A]/g, "\'");
s = s.replace(/[\u201C|\u201D|\u201E]/g, "\"");
s = s.replace(/\u2026/g, "...");
s = s.replace(/[\u2013|\u2014]/g, "-");
s = s.replace(/\u02C6/g, "^");
s = s.replace(/\u2039/g, "<");
s = s.replace(/\u203A/g, ">");
s = s.replace(/[\u02DC|\u00A0]/g, " ");
document.getElementById("textcounter3").value = s;}
</script>

1 个答案:

答案 0 :(得分:1)

确实有:

function replaceWordChars(text,textcounter) {
var s = text;
s = s.replace(/[\u2018|\u2019|\u201A]/g, "\'");
s = s.replace(/[\u201C|\u201D|\u201E]/g, "\"");
s = s.replace(/\u2026/g, "...");
s = s.replace(/[\u2013|\u2014]/g, "-");
s = s.replace(/\u02C6/g, "^");
s = s.replace(/\u2039/g, "<");
s = s.replace(/\u203A/g, ">");
s = s.replace(/[\u02DC|\u00A0]/g, " ");
document.getElementById(textcounter).value = s;
}

称之为:

replaceWordChars(text,"textcounter1");
replaceWordChars(text,"textcounter2");
replaceWordChars(text,"textcounter3");
来自keyUp事件的

<... keyUp="replaceWordChars(this.value,this.id)" ...>