Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

dstoianov891 avatar dstoianov891 18 Точки

03 Match Dates JavaScript Fundamentals LAB Regex

Здравейте, 

 

Имам проблем с трета задача от лекцията. Не ми приема регекс модела, а той е същия като от условието в домашното?

https://pastebin.com/D8Vp1LRK

 

3.Match Dates

Write a program, which matches a date in the format "dd{separator}MMM{separator}yyyy".

Compose the Regular Expression

Every valid date has the following characteristics:

  • Always starts with two digits, followed by a separator
  • After that, it has one uppercase and two lowercase letters (e.g. Jan, Mar).
  • After that, it has a separator and exactly 4 digits (for the year).
  • The separator could be either of three things: a period ("."), a hyphen ("-") or a forward slash ("/")
  • The separator needs to be the same for the whole date (e.g. 13.03.2016 is valid, 13.03/2016 is NOT). Use a group backreference to check for this.

You can follow the table below to help with composing your RegEx:

Match ALL of these

Match NONE of these

13/Jul/1928, 10-Nov-1934, 25.Dec.1937

01/Jan-1951, 23/sept/1973, 1/Feb/2016

Use named capturing groups for the day, month and year.

Since this problem requires more complex RegEx, which includes named capturing groups, we’ll take a look at how to construct it:

  • First off, we don’t want anything at the start of our date, so we’re going to use a word boundary "\b":
     
  • Next, we’re going to match the day, by telling our RegEx to match exactly two digits, and since we want to extract the day from the match later, we’re going to put it in a capturing group:

    We’re also going to give our group a name, since it’s easier to navigate by group name than by group index:
     
  • Next comes the separator – either a hyphen, period or forward slash. We can use a character class for this:

    Since we want to use the separator we matched here to match the same separator further into the date, we’re going to put it in a capturing group:
     
  • Next comes the month, which consists of a capital Latin letter and exactly two lowercase Latin letters:
     
  • Next, we’re going to match the same separator we matched earlier. We can use a backreference for that:
     
  • Next up, we’re going to match the year, which consists of exactly 4 digits:
     
  • Finally, since we don’t want to match the date if there’s anything else glued to it, we’re going to use another word boundary for the end:
     

Now it’s time to find all the valid dates in the input and print each date in the following format: "Day: {day}, Month: {month}, Year: {year}", each on a new line.

Implement the Solution in JavaScript

First off, we’re going to put our RegEx in a variable and get the matches from the string:

 

Next, we’re going to iterate over every single element in the array and extract the day, month and year by making new patterns and matching them:

 

Examples

Input

"13/Jul/1928, 10-Nov-1934, , 01/Jan-1951,f 25.Dec.1937 23/09/1973, 1/Feb/2016"

Output

Day: 13, Month: Jul, Year: 1928

Day: 10, Month: Nov, Year: 1934

Day: 25, Month: Dec, Year: 1937

 

0
JavaScript Fundamentals 19/07/2019 14:04:14
willystyle avatar willystyle 2472 Точки
Best Answer

ред 14 ти е излишен, останал е сигурно от предишната задача :)

0
dstoianov891 avatar dstoianov891 18 Точки

Благодаря! Мисля че проблема е, че на работа имаме стара версия на node.js и групите на Regex не работят. В къщи съм с последна версия на node.js и нямам проблем с групите :) 

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.