Javascript date formatting and regular expression Regexp. $ usage

Source: Internet
Author: User

if you are free today, you can write a calendar. on the Internet, you can see the Code formatted with a javascript date, A little dizzy after reading it. Code:

/** <Br/> * @ Param d the delimiter <br/> * @ Param p the pattern of your date <br/> * @ author meizz <br/> *@ author kimsoft add W + pattern <br/> */<br/> date. prototype. format = function (style) {<br/> var o = {<br/> "m +": This. getmonth () + 1, // month <br/> "d +": This. getdate (), // day <br/> "H +": This. gethours (), // hour <br/> "m +": This. getminutes (), // minute <br/> "s +": This. getseconds (), // second <B R/> "W +": "/u65e5/u4e00/u4e8c/u4e09/u56db/u4e94/u516d ". charat (this. getday (), // week <br/> "q +": math. floor (this. getmonth () + 3)/3), // quarter <br/> "S": This. getmilliseconds () // millisecond <br/>}< br/> If (/(Y + )/. test (style) {<br/> style = style. replace (Regexp. $1, (this. getfullyear () + ""). substr (4-Regexp. $1. length); <br/>}< br/> for (var k in O) {<br/> If (New Regexp ("(" + K + ") "). test (Style) {<Br/> style = style. Replace (Regexp. $1, Regexp. $1. Length = 1? O [k]: ("00" + O [k]). substr ("" + O [k]). length); <br/>}< br/> return style; <br/> };

The above code shows Regexp. $1. I checked it to find out:

$1... $9 is an attribute of Regexp, $1... $9: indicates the matching result of group N, which is useful when multiple groups in the regular expression are grouped.

In JScript, A String object related to a regular expression is called replace. But how to use a regular expression to match the result in replace?

In this case, we need to use the "matching variable". The matching variable is used to represent the result of regular expression matching. The following describes the matching variables:
$ & -- Indicates the matching results of all matching groups. The matching group is the () Group of the regular expression.
$ -- Represents $ characters. Because $ characters are used for matching variables, escape
$ N -- similar to the previous $1... $9 indicates the result of group N matching
$ NN -- the result of the NN group match is very simple.
$ '-- The leftcontext mentioned above. For example, if abcdefg is matched with D, ABC is its leftcontext.
$ '-- It is very close to the above and should not be mistaken !, This is rightcontext.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.