Home  >  Article  >  Backend Development  >  字符串截取有关问题

字符串截取有关问题

WBOY
WBOYOriginal
2016-06-13 13:52:36722browse

字符串截取问题
19850523怎么变成1985-05-23这样的形式呢?
我现在的做法是用了三次substr()
有简单的办法么?

------解决方案--------------------
正则表达式:

var s = "19850516 ";
s = s.replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3 ");
alert(s);
------解决方案--------------------
看样子是日期字符串,不妨利用date函数
$str = '19850523 ';
echo date( 'Y-m-d ',strtotime($str));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn