简体   繁体   English

在字符串Javascript中搜索希腊字母

[英]Searching for a Greek letter in a string Javascript

I'm trying to figure out a way to search for a Greek letter in a string replace it with another character. 我试图找出一种方法来搜索字符串中的希腊字母,然后用另一个字符替换它。 For example 例如

value = "Hello μ!";
value.replace("μ", "You");
alert(value);

//Alerted value should be "Hello You!"

Unfortunately this isn't working. 不幸的是,这不起作用。 I tried searching for answers online but to no avail. 我尝试在线搜索答案,但无济于事。 Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

You have to reassign the value. 您必须重新分配值。 The replace function does not modify the existing value because strings are immutable in Javascript. 替换功能不会修改现有值,因为字符串在Javascript中是不可变的。 This works: 这有效:

value = value.replace("μ", "You")

 value = "Hello μ!"; value = value.replace("μ", "You"); alert(value); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM