开发者社区 问答 正文

JavaScript获取URL中一个参数并将首字母转为大写

function getPageCly() 
{
    var reg = new RegExp("oschina|home|hello|world");
    var cly = window.location.href.match(reg);
    if(cly==null)
        cly="oschina";
     
    return cly.substring(0,1).toUpperCase()+cly.substring(1);
}

我要实现的是获取URL中一个参数并将首字母转为大写,结果只有url不匹配正则式时才实现了,错在哪阿?

展开
收起
a123456678 2016-07-15 15:45:56 2389 分享 版权
1 条回答
写回答
取消 提交回答
  • function getPageCly() {
        var reg = new RegExp("oschina|home|hello|world");
        var cly = window.location.href.match(reg);
        if(cly==null){
            return 'Oschina';
        }
        return cly[0].substring(0,1).toUpperCase()+cly[0].substring(1);
    }
    2019-07-17 19:57:07
    赞同 展开评论