简体   繁体   English

在字符串中短划线后大写字母最简单的方法是什么?

[英]What's the easiest way to capitalize letters after dash in a string?

I want to transform a string like config-option into configOption . 我想将config-option之类的字符串转换为configOption What will be the easiest way? 最简单的方法是什么?

Instead of match[1] , I'd recommend match.charAt(1) (see string.charAt(x) or string[x]? ): 我建议使用match.charAt(1)代替match[1] (请参阅string.charAt(x)或string [x]? ):

str.replace(/-./g, function(match) {return match.charAt(1).toUpperCase();})

Alternatively, you can use a group in your regex: 另外,您可以在正则表达式中使用组:

str.replace(/-(.)/g, function(m, c) {return c.toUpperCase();})

我只使用了正则表达式,并将替换指定为函数而不是字符串。

str.replace(/-./g, function(match) {return match[1].toUpperCase();})

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

相关问题 我的代码有什么问题? (编码字节大写字母) - What's wrong with my code? (coderbyte capitalize letters) 将数组中的字符串的首字母大写 - Capitalize first letters of string in an array 删除字符串之间的短划线并在Javascript中大写字符串 - Remove dash between strings and Capitalize the String in Javascript 部署Meteor应用最简单的方法是什么? - What's the easiest way to deploy Meteor app? 用&#39;替换&#39;\\ n&#39;最简单的方法是 <br> &#39;? - What's the easiest way to replace '\n' with '<br>'? 获取复选框值的最简单方法是什么 - What's easiest way to get value of a checkbox 重定向不受支持的浏览器的最简单方法是什么? - What's the easiest way to redirect unsupported browsers? 将服务器返回的字符串分配给javascript字符串最简单的方法是什么? - What's the easiest way to assign a string returned from the server to a javascript string? 在库加载后将JS排队运行的最简单方法是什么? (页面末尾的jQ) - What's the easiest way to queue JS to run after a library loads? (jQ at end of page) 在 python 中下载网页源的最简单方法是什么? (javascript 已应用后) - What's the easiest way to download the source of a webpage in python? (After javascript has been applied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM