【发布时间】:2021-03-05 01:35:40
【问题描述】:
我正在尝试在我的作业中使用正则表达式。
这里有2个我无法解决的问题:
问题 1:第一个 if 语句旨在将字符串放入“pascal”大小写。第二个 if 语句变成“骆驼”的情况。不管是哪条语句,看起来执行在第一条语句之后都会停止。我在这里做错了什么?
问题 2:我的第一个 if 语句即使返回结果(“pascal”)我也无法弄清楚如何在代码的正则表达式部分中去除空格。
请帮忙。连续两天卡住了。
const makeCase = function(input, type) {
//place your code here
let inType = type;
let finalInput = (type === inType)
if(finalInput) {
return input.replace(/^(.)|\s+(.)/g, function(str){
return str.toUpperCase();
});
} else if(finalInput) {
return input.replace(/\W+(.)/g, function(str){
return str.toUpperCase();
});
}
};
console.log(makeCase("this is a string", "pascal"));
console.log(makeCase("this is a string", "camel"));
【问题讨论】:
标签: javascript regex