![](/img/trans.png)
[英]Trying to find a more efficient way to do multiple replaces without repeating the same code in jQuery
[英]A nicer way to do multiple replaces?
var type = event.type.replace('page', 'Page').replace('init', 'Init').replace('before', 'Before').replace('show', 'Show').replace('hide', 'Hide');
誰能想到一種更好的方法?
您可以將RegExp與.replace()
,並將函數作為第二個參數傳遞(=純JavaScript):
var type = event.type.replace(/page|init|before|show|hide/, function(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.