用.replace("要被置換的字","要換成的字")

只會置換第一個

若要置換全部,必須用//g (golobal)

Ex: 

var test = "B1 B2 S1/S2//End";
console.log(test);

=>結果: B1 B2  S1/S2//End

test = test.replace(" ", "-").replace("/", "-");
console.log(test);

=>結果: B1-B2  S1-S2//End

test = test.replace(/ /g, "-").replace(/\//g, "-");
console.log(test);

=>結果:B1-B2--S1-S2--End

 

創作者介紹
創作者 廣度與深度 的頭像
sendohlun

廣度與深度

sendohlun 發表在 痞客邦 留言(0) 人氣( 22 )