欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 日期字符串截取分割成單個(gè)具體的日期(2009-12-30 13:28:29)

 更新時(shí)間:2009年12月16日 19:52:44   作者:  
js 日期字符串截取分割,這里利用的indexOf查找字符串的方法,效率什么的都不是很高,大家可以用數(shù)組的方式,將空格,分號(hào),連接符號(hào)統(tǒng)一替換成一個(gè)樣的字符,分割。

下面是用數(shù)組+正則替換實(shí)現(xiàn)的代碼

"hand hand hand"想變換為"hand.gif hand.gif hand.gif"
開(kāi)始用
str=str.replace("hand","hand.gif");
輸出:hand.gif hand hand
只替換了一次。。。:(
想到用正則,因?yàn)閞eplace本來(lái)就可以用正則替換。

引用
返回根據(jù)正則表達(dá)式進(jìn)行文字替換后的字符串的復(fù)制。

stringObj.replace(rgExp, replaceText)


于是寫(xiě)
str = str.replace(/hand/,"hand.gif")
無(wú)效。。。
全部替換要加g,
str = str.replace(/hand/g,"hand.gif")
還是不行:(

參考了JavaScript的replace方法與正則表達(dá)式結(jié)合應(yīng)用講解這篇文章后,終于明白,原來(lái)要用()括起來(lái),才會(huì)替換()里的東東。正確的寫(xiě)法如下:
str = "hand hand hand";
str=str.replace(/(hand)/g,"hand.gif");
document.write(str);
正確輸出:hand.gif hand.gif hand.gif。

JS的正則另一種寫(xiě)法是使用RegExp:
如str=str.replace(/(hand)/g,"hand.gif");
等同于:
reg = new RegExp("(hand)","g");
str = str.replace(reg,'hand.gif');
reg需要?jiǎng)討B(tài)生成時(shí)更適合使用這種方式。

擴(kuò)展一下:
str = "hand'( hand'( hand'(";
str=str.replace(/(hand\'\()/g,"hand.gif");
document.write(str);

str = 'hand\'( hand\'( hand\'(';
str=str.replace(/(hand\'\()/g,"hand.gif");
document.write(str);

相關(guān)文章

最新評(píng)論