将短划线字符串转换为camelCase? [英] Convert dash-separated string to camelCase?

查看:59
本文介绍了将短划线字符串转换为camelCase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我总是有一个由 - 分隔的字符串。有没有办法改变

For example suppose I always have a string that is delimited by "-". Is there a way to transform

今天很棒的一天

itIsAGreatDayToday

itIsAGreatDayToday

使用RegEx?

推荐答案

已编辑以支持非小写输入和Unicode)

Yes (edited to support non-lowercase input and Unicode):

function camelCase(input) { 
    return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
        return group1.toUpperCase();
    });
}

在MDN的将功能指定为参数文档。

回调函数的第一个参数是完全匹配,后续参数是正则表达式中的括号组(在这种情况下,是连字符后面的字符)。

The first argument to the callback function is the full match, and subsequent arguments are the parenthesized groups in the regex (in this case, the character after the the hyphen).

这篇关于将短划线字符串转换为camelCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆