删除特殊符号和额外空格,并使其成为驼峰大小写javascript?
我有这个函数来删除特殊符号和额外的空格,但把字符串转换成驼峰大小写?范例删除特殊符号和额外空格,并使其成为驼峰大小写javascript?,javascript,regex,Javascript,Regex,我有这个函数来删除特殊符号和额外的空格,但把字符串转换成驼峰大小写?范例 kitchener\waterloo to kitchenerWaterloo St. Thomas to stThomas Owen Sound/Walkerton toowenSoundWalkerton javascript代码 countyName.toLowerCase().replace(/[^A-Z0-9]+/ig,''); 我们可以通过将求反类转换为普通字符类,然后对它们使用Array.map来匹
kitchener\waterloo to kitchenerWaterloo
St. Thomas to stThomas
Owen Sound/Walkerton toowenSoundWalkerton
javascript代码
countyName.toLowerCase().replace(/[^A-Z0-9]+/ig,'');
我们可以通过将求反类转换为普通字符类,然后对它们使用
Array.map
来匹配所需的内容
countyName = countyName.toLowerCase().match(/[A-Z0-9]+/ig).map(function(word, i){
if(!i) return word;
return word[0].toUpperCase() + word.slice(1); // return newly formed string
}).join("");
您可以使用replace with函数来调用要更改的字母的大写字母
var countyName = "Owen Sound/Walkerton";
var result = countyName
.trim() //might need polyfill if you need to support older browsers
.toLowerCase() //lower case everything
.replace(/([^A-Z0-9]+)(.)/ig, //match multiple non-letter/numbers followed by any character
function(match) {
return arguments[2].toUpperCase(); //3rd index is the character we need to transform uppercase
}
);
console.log(result);
现在,如果字符串前面有一个特殊字符,这将失败。因此,修剪方法需要替换为
.replace(/^[^A-Z0-9]+/gi,"")
@图坦卡蒙,没有任何空格。我明白了,但问题是:'圣托马斯到圣托马斯'@solidus.snake checknow@tutankhamun是的。。你是对的。没有正确地阅读问题。@epascarello不,不是,按照骆驼案的意思。