开发者

Replace x, y and z to w, u, and i in one replace

开发者 https://www.devze.com 2023-01-12 09:00 出处:网络
I\'m开发者_如何学编程 not so good at javascript and it\'s replace, so I\'m wondering if you can optimize this line (etc one .replace instead of two):

I'm开发者_如何学编程 not so good at javascript and it's replace, so I'm wondering if you can optimize this line (etc one .replace instead of two):

$itemClicked.replace(/ä|å/g, 'a').replace(/ö/g, 'o');


This should be the best possible if you only need to replace just these 3 letters. Using 1 .replace is possible, but it must use a function, e.g.

$itemClicked.replace(/[äåö]/g, function(s) { return s=='ö'?'o':'a'; });

See JavaScript: Efficiently replace all accented characters in a string? for a generalization.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号