javascript extract date via regular expression

前端 未结 3 1581
说谎
说谎 2020-12-12 02:30

I don\'t know much about regular expressions, but I got a string (url) and I\'d like to extract the date from it:

var myurl = \"https://example.com/display         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-12 03:08

    Depending on how the URL can change, you can use something like:

    \/\d{4}\/\d{2}\/\d{2}\/
    

    The above will extract /2010/07/06/ (the two slashes just to be safer - you can remove the heading and trailing \/ to get just 2010/07/06, but you might have issues if URL contains other parts that may match).

    See the online regexp example here:

    • http://rubular.com/r/bce4IHyCjW

    Here's the jsfiddle:

    • http://jsfiddle.net/zwkDQ/

    To format it, take a look e.g. here:

    • http://blog.stevenlevithan.com/archives/date-time-format

    Something along these lines (note you need the function from above):

    var dt = new Date(2010, 6, 6);
    dateFormat(dt, "dS of mmmm, yyyy");
    // 6th of June, 2010
    

提交回复
热议问题