当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 日期字符串截取分割成单个具体的日期(2009-12-30 13:28:29)

Javascript
[IE&FireFox兼容]JS对select操作
JS实现全景图效果360度旋转
Unicode 编码转换器
如何用javascript判断录入的日期是否合法
图片从右至左滚动JS
JS控件autocomplete 0.11演示及下载 1月5日已更新
一个对于js this关键字的问题
Javascript标准DOM Range操作全集
兼容Mozilla必须知道的知识。
你所要知道JS(DHTML)中的一些技巧
JS效率个人经验谈(8-15更新),加入range技巧
如何让动态插入的javascript脚本代码跑起来。
Javascript调试工具(下载)
脚本中出现 window.open() access is denied - 拒绝访问 情况一则及分析
Javascript-Mozilla和IE中的一个函数直接量的问题
贴一个在Mozilla中常用的Javascript代码
Javascript miscellanea -display data real time, using window.status
js技巧--转义符"\"的妙用
In Javascript Class, how to call the prototype method.(three method)
Javascript与vbscript数据共享

Javascript 中的 js 日期字符串截取分割成单个具体的日期(2009-12-30 13:28:29)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 204 ::
收藏到网摘: n/a

js 日期字符串截取分割,这里利用的indexOf查找字符串的方法,效率什么的都不是很高,大家可以用数组的方式,将空格,分号,连接符号统一替换成一个样的字符,分割。

[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

下面是用数组+正则替换实现的代码

[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

"hand hand hand"想变换为"hand.gif hand.gif hand.gif"
开始用
str=str.replace("hand","hand.gif");
输出:hand.gif hand hand
只替换了一次。。。:(
想到用正则,因为replace本来就可以用正则替换。
引用
返回根据正则表达式进行文字替换后的字符串的复制。
stringObj.replace(rgExp, replaceText)

于是写
str = str.replace(/hand/,"hand.gif")
无效。。。
全部替换要加g,
str = str.replace(/hand/g,"hand.gif")
还是不行:(
参考了JavaScript的replace方法与正则表达式结合应用讲解这篇文章后,终于明白,原来要用()括起来,才会替换()里的东东。正确的写法如下:
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需要动态生成时更适合使用这种方式。
扩展一下:
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);