我尝试过逃跑,替换,分手和加入,但我似乎无法摆脱这种情况。在类别标题中:
Over 60's
var dropdown = document.getElementById("cat");
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
var currentText = this.options[this.selectedIndex].innerHTML;
var currentSlug = currentText.replace(/ |'/g, function(match){ if (match === " " || match === "'" ){ return "-"; } return "";}).toLowerCase();
location.href = "..url.. " + currentSlug;
} else {
location.href = dropdown.baseURI;
}
}
dropdown.onchange = onCatChange;
此场景中是否需要不同的方法
答案 0 :(得分:0)
如果要将字符列表替换为短划线,则应将字符放在方括号[]
内。对于撇号,用反斜杠来逃避它。
var s ="Text|Text'Text Text";
// replace all instances of space, pipe and apostrophe to dash
// and convert the string to lowercase
var t = s.replace(/[ |\']/g,'-').toLowerCase();
// use t here
alert(t);