亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

stringObj.replace(rgExp, replaceText)


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

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

JS的正則另一種寫法是使用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)文章

最新評論