删除 javascript 字符串中的元音变音或特殊字符
以前从未在 javascript 字符串中使用变音符号或特殊字符。我的问题是如何删除它们?
例如,我在 javascript 中有这个:
var oldstr = "Bayern München";
var str = oldstr.split(' ').join('-');
结果是 Bayern-München ok easy,但现在我想删除元音变音或特殊字符,例如:
希洪皇家竞技队。
我怎样才能意识到这一点?
亲切的问候,
弗兰克
Never played before with umlauts or specialchars in javascript strings. My problem is how to remove them?
For example I have this in javascript:
var oldstr = "Bayern München";
var str = oldstr.split(' ').join('-');
Result is Bayern-München ok easy, but now I want to remove the umlaut or specialchar like:
Real Sporting de Gijón.
How can I realize this?
Kind regards,
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个名为“remove-accents”的 npm 包。
npm i remove-accents
。函数:
import { remove } from "remove-accents";
remove(inputString)
Theres a npm package called "remove-accents".
npm i remove-accents
.function:
import { remove } from "remove-accents";
remove(inputString)
replace
应该能够为您做到这一点,例如:...当然
ü
和u
是不是同一封信。 :-)如果您尝试用某些内容(例如
-
)替换给定范围之外的所有字符,您可以通过指定范围来实现:替换全部非英文字母、数字、
-
或带有-
的_
的字符。 (字符范围是[...]
位,开头的^
表示“不”。) 这是一个实例。但这个(“Bayern-M-nchen”)对于慕尼黑先生来说可能有点不舒服。 :-) 您可以使用传递到
replace
的函数来尝试删除变音符号:Live example
以上针对长字符串进行了优化。如果字符串本身很短,那么重复的正则表达式可能会更好:
...但这是推测性的。
请注意,JavaScript 字符串中的文字字符完全没问题,但文件编码可能会带来乐趣。我倾向于坚持使用 unicode 转义。例如,上面的内容是:
……但是,还有很多事情要做……
replace
should be able to do it for you, e.g.:...of course
ü
andu
are not the same letter. :-)If you're trying to replace all characters outside a given range with something (like a
-
), you can do that by specifying a range:That replaces all characters that aren't English letters, digits,
-
, or_
with-
. (The character range is the[...]
bit, the^
at the beginning means "not".) Here's a live example.But that ("Bayern-M-nchen") may be a bit unpleasant for Mr. München to look at. :-) You could use a function passed into
replace
to try to just drop diacriticals:Live example
The above is optimized for long strings. If the string itself is short, you may be better off with repeated regexps:
...but that's speculative.
Note that literal characters in JavaScript strings are totally fine, but you can run into fun with encoding of files. I tend to stick to unicode escapes. So for instance, the above would be:
...but again, there are a lot more to do...