Jquery字符串替换,只替换数组的开头

时间:2013-11-14 18:50:13

标签: javascript jquery replace

我这里有这个代码:

<script type="text/javascript">
    var catURL = window.location.pathname;
    var catTitle = catURL.split("/");
    catTitleNew = catTitle[4].replace("-", " ");
    alert(catTitleNew);
    //this will alert will convert this-is-my-title to this is-my-title
</script>

我不认为我的替换代码有效,它只替换第一个"-"如何让它替换所有"-"

1 个答案:

答案 0 :(得分:4)

catTitleNew = catTitle[4].replace(/-/g, " ");

Fiddle