javascript 在javascript中替换字符串中的多个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11782481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:20:03  来源:igfitidea点击:

Replace multiple characters in a string in javascript

javascripthtmlvariablescookiesreplace

提问by SeinopSys

I got this nice code, which I have no idea why doesn't work. It should get the value of a text input and replace each given national character with it's HTML code, for compatibility purposes. But, when I click the button, the function returns the string without any changes. Any idea?

我得到了这个很好的代码,我不知道为什么不起作用。出于兼容性目的,它应该获取文本输入的值并将每个给定的国家字符替换为其 HTML 代码。但是,当我单击按钮时,该函数返回字符串而没有任何更改。任何的想法?

(jsfiddle)

( jsfiddle)

<a id="reminder1" onclick="document.getElementById('reminder2').style.display = ''; document.getElementById('reminder1').style.display = 'none';">
    Set reminder
</a>
<a id="reminder2" class="reminder" style="display:none;">
    <input type="text" id="reminderh" size=40 style="font-size:20px;">
    <input type="button" value="Set" onclick="csere(document.getElementById('reminderh').value);">
</a>

<script>
function csere(qwe){
document.getElementById('reminder2').style.display = 'none';

var rtz0  = qwe.replace("á","&aacute;");
var rtz1  = rtz0.replace("á","&Aacute;");

var rtz2  = rtz1.replace("é","&eacute;");
var rtz3  = rtz2.replace("é","&Eacute;");

var rtz4  = rtz3.replace("í","&iacute;");
var rtz5  = rtz4.replace("í","&Iacute;");

var rtz6  = rtz5.replace("?","&ouml;");
var rtz7  = rtz6.replace("?","&Ouml;");
var rtz8  = rtz7.replace("?","&&#337;");
var rtz9  = rtz8.replace("?","&#336;");
var rtz10 = rtz9.replace("ó","&oacute;");
var rtz11 = rtz10.replace("ó","&Oacute;");

var rtz12 = rtz11.replace("ü","&uuml;");
var rtz13 = rtz12.replace("ü","&Uuml;");
var rtz14 = rtz13.replace("?","&#369;");
var rtz15 = rtz14.replace("?","&#368;");
var rtz16 = rtz15.replace("ú","&uacute;");
var uio = rtz16.replace("ú","&Uacute;");

//Creates a cookie with the final value (different function)
createCookie('reminder',uio,1500);

document.getElementById('reminder1').style.display = '';
}
</script>

采纳答案by Bergi

You can just replaceeverything programmatically, not using named entities:

您可以编程方式替换所有内容,而不是使用命名实体:

return input.replace(/[^ -~]/g, function(chr) {
//                    ^^^^^^ 
// this is a regexp for "everything than printable ASCII-characters"
// and even works in a ASCII-only charset. Identic: [^\u0020-\u007E]
    return "&#"+chr.charCodeAt(0)+";";
});

If you want to use named entities, you can combine this with a key-value-map (as like in @Hymanwanders answer):

如果你想使用命名实体,你可以将它与键值映射结合起来(就像@Hymanwanders 的回答一样):

var chars = {
    "á" : "&aacute;",
    "á" : "&Aacute;",
    "é" : "&eacute;",
    "é" : "&Eacute;",
    ...
}
return input.replace(/[^ -~]/g, function(chr) {
    return (chr in chars) 
      ? chars[chr]
      : "&#"+chr.charCodeAt(0)+";";
});


However, you should never need to use html entities in JavaScript. Use UTF8 as the character encoding for everything, and it will work.

但是,您永远不需要在 JavaScript 中使用 html 实体。使用 UTF8 作为所有内容的字符编码,它会起作用。

回答by Hymanwanders

You could create an object that has key/value pairs for each character to replace:

您可以创建一个对象,该对象具有要替换的每个字符的键/值对:

var chars = {
    "á" : "&aacute;",
    "á" : "&Aacute;",
    "é" : "&eacute;",
    "é" : "&Eacute;",
    ...
}

And then use a function in your .replacecall:

然后在您的.replace通话中使用一个函数:

var uio = qwe.replace(/[ááéé]/g,function(c) { return chars[c]; });

Your object and regular expression will obviously need to grow to include all the characters you want to replace

您的对象和正则表达式显然需要增长以包含您要替换的所有字符

回答by Micah Henning

The characters are subject to the encoding of the HTML page, the JavaScript page, and the HTTP request. Try replacing the characters with their Unicode equivalents:

字符受制于 HTML 页面、JavaScript 页面和 HTTP 请求的编码。尝试用它们的 Unicode 等价物替换字符:

<a id="reminder1" onclick="document.getElementById('reminder2').style.display = ''; document.getElementById('reminder1').style.display = 'none';">
    Set reminder
</a>
<a id="reminder2" class="reminder" style="display:none;">
    <input type="text" id="reminderh" size=40 style="font-size:20px;">
    <input type="button" value="Set" onclick="csere(document.getElementById('reminderh').value);">
</a>

<script>
function csere(qwe){
document.getElementById('reminder2').style.display = 'none';

var rtz0  = qwe.replace(/\u00E1/,"&aacute;");
var rtz1  = rtz0.replace(/\u00C1/,"&Aacute;");

var rtz2  = rtz1.replace(/\u00E9/,"&eacute;");
var rtz3  = rtz2.replace(/\u00C9/,"&Eacute;");

var rtz4  = rtz3.replace(/\u00ED/,"&iacute;");
var rtz5  = rtz4.replace(/\u00CD/,"&Iacute;");

var rtz6  = rtz5.replace(/\u00F6/,"&ouml;");
var rtz7  = rtz6.replace(/\u00D6/,"&Ouml;");
var rtz8  = rtz7.replace(/\u00F5/,"&&#337;");
var rtz9  = rtz8.replace(/\u00D5/,"&#336;");
var rtz10 = rtz9.replace(/\u00F3/,"&oacute;");
var rtz11 = rtz10.replace(/\u00D3/,"&Oacute;");

var rtz12 = rtz11.replace(/\u00FC/,"&uuml;");
var rtz13 = rtz12.replace(/\u00DC/,"&Uuml;");
var rtz14 = rtz13.replace(/\u0171/,"&#369;");
var rtz15 = rtz14.replace(/\u0170/,"&#368;");
var rtz16 = rtz15.replace(/\u00FA/,"&uacute;");
var uio = rtz16.replace(/\u00DA/,"&Uacute;");

//Creates a cookie with the final value (different function)
createCookie('reminder',uio,1500);

document.getElementById('reminder1').style.display = '';
}
</script>

Double check my conversions to be sure. I used the grid on Wikibooks.

仔细检查我的转换以确保。我在Wikibooks上使用了网格。

回答by Josh Mein

I think you are having an issue with only replacing the first instance of a character. In javascript you have to specifiy global replaces using regex like this:

我认为您在仅替换字符的第一个实例时遇到了问题。在 javascript 中,您必须像这样使用正则表达式指定全局替换:

var rtz0  = qwe.replace(new RegExp("á", "g"), "&aacute;");

It would be best to create an array as mentioned by PPvG or Hymanwanders, but otherwise atleast reuse the existing variable. You could easily do it like this:

最好创建一个 PPvG 或 Hymanwanders 提到的数组,否则至少重用现有变量。你可以很容易地这样做:

qwe  = qwe.replace(new RegExp("á", "g"), "&aacute;");
qwe  = qwe.replace(new RegExp("á", "g"), "&Aacute;");