admin管理员组文章数量:1336644
I am implementing a chat-function to a JavaScript game using WebSocket. I want to replace non-ascii characters the user has written in the input textfield with other letters. Ä is replaced with a and Ö is replaced with o. And every other non-ascii characters should be replaced by "".
var message = document.getElementById("write_message").value;
message = message.replace(/ä/g, "a").replace(/ö/g, "o");
message = message.replace(/^[\000-\177]/g, "");
ws.send("M" + message);
I tried even simpler versions of the above code, but somehow all user input seemed to be replaced. Even ascii-characters. I found the regular expression from another Stackoverflow question.
I am implementing a chat-function to a JavaScript game using WebSocket. I want to replace non-ascii characters the user has written in the input textfield with other letters. Ä is replaced with a and Ö is replaced with o. And every other non-ascii characters should be replaced by "".
var message = document.getElementById("write_message").value;
message = message.replace(/ä/g, "a").replace(/ö/g, "o");
message = message.replace(/^[\000-\177]/g, "");
ws.send("M" + message);
I tried even simpler versions of the above code, but somehow all user input seemed to be replaced. Even ascii-characters. I found the regular expression from another Stackoverflow question.
Share Improve this question asked Apr 17, 2013 at 14:13 Teemu LeivoTeemu Leivo 3412 gold badges5 silver badges20 bronze badges 1-
encodeURIComponent
? – BlueRaja - Danny Pflughoeft Commented Oct 27, 2017 at 18:51
2 Answers
Reset to default 4you have to know the charset of the supporting html page. depending on whether it's unicode or some 8bit charser, use \uzzzz
or \xzz
to match chars where z
represents a hex digit.
example: message = message.replace(/^[\u0080-\uffff]/g, "");
ascii-fies unicode text.
Your code is correct except that the circumflex ^
must appear after the left bracket [
in order to indicate negation. Otherwise it means something pletely different.
However, I think you actually want to map uppercase Ä and Ö to A and O instead of deleting them. For this, you would use
message = message.replace(/ä/g, "a")
.replace(/ö/g, "o")
.replace(/Ä/g, "A")
.replace(/Ö/g, "O")
.replace(/[^\000-\177]/g, "");
本文标签: htmlHow to replace nonAscii characters from input with something else in JavaScriptStack Overflow
版权声明:本文标题:html - How to replace non-Ascii characters from input with something else in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408541a2469317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论