javascript - Can Someone Explain this regex snippet? -
i found replace code looks regular expressions in them can't decipher
new date().toisostring(). replace(/[z|t|\.]/gim," "). replace(/\d{3}\s$/gim, " "). replace(/:\d{2}\s+$/, " "). trim() + "\n")
sorry if that's vague. i'm not sure looking at
the code
new date().toisostring()
generates looks this
2015-07-01t17:21:22.123z
the regex put format
2015-07-01 17:21
all of regex has flags g
, m
, , i
. straight regex101 means:
g modifier: global. matches (don't return on first match)
i modifier: insensitive. case insensitive match (ignores case of [a-za-z])
m modifier: multi-line. causes ^ , $ match begin/end of each line (not begin/end of string)
the first regex
[z|t|\.]
actually has mistake. whoever wrote assumed |
means or
not case inside square brackets. better written as
[zt\.]
this match 2015-07-01t
17:21:22.
123z
, replace
replace(/[z|t|\.]/gim," ")
will replace every character matches regex space. giving new string
2015-07-01 17:21:22 123
the next regex \d{3}\s$
matches , replaces 2015-07-01 17:21:22 123
, giving you:
2015-07-01 17:21:22
finally regex :\d{2}\s+$
matches , replaces 2015-07-01 17:21:22
, giving you
2015-07-01 17:21
and spaces trimmed off.
all in not great of way want. either better substrings or momentjs else said.
Comments
Post a Comment