简体   繁体   中英

JavaScript RegEx to replace repeat characters of more than 2

I have an string, like YYYY/MMM/DD and I want to convert it into YY/MM/DD formart. In short, I want to replace repeated characters more than 2.

Example 01:- YYYY/MMM/DD -> YY/MM/DD Example 02 :- MMM/YYYY/DD -> MM/YY/DD

Please help me out.

可以做到这一点,使用反向引用查找3个以上相同字符的序列,然后仅用两个替换它们:

str.replace(/(.)\1{2,}/g, '$1$1')

try to use this

    var date = new Date();
    var datestr = ('0' + date.getDate()).substr(-2, 2) + '/' + ('0' + date.getMonth()).substr(-2, 2) + '/' + ('0' + date.getFullYear()).substr(-2, 2);
    alert(datestr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM