JS-Fundamentals-Regular-Expressions-Lab
JS-Fundamentals-Regular-Expressions-Lab
Problems for exercise and homework for the "JS Fundamentals" Course @ SoftUni.
Submit your solutions in the SoftUni judge system at: https://judge.softuni.org/Contests/1708
After you’ve constructed your regular expression, it’s time to write the solution in JavaScript.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 1 of 5
Note: You should put "/" before and after the pattern so that it is interpreted as a RegEx pattern. Also, place the
'g' (global) flag after it, so that you get all the matches in the text.
Now, it’s time to read the input, to extract the matches from it and push them into an array. For this we can use
exec():
The exec method matches the string and the pattern keeps the first index after the match. This way the next time
exec runs it starts looking after the last match. If there are no more matches, it will return null.
We are declaring a variable in the while loop's condition because we need to check every time if there are any more
matches.
Now we have an array (validNames), which holds all of the valid names in the input. All that is left is to join it by
space and print it (do this by using join()):
Examples
Input
"Ivan Ivanov, Ivan ivanov, ivan Ivanov, IVan Ivanov, Test Testov, Ivan Ivanov"
Output
Ivan Ivanov Test Testov
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 2 of 5
Ensure that before the '+' sign there is either a space or the beginning of the string.
You can use the following table of values to test your RegEx against:
After that, let’s make an array of matches using like in the previous exercise:
Examples
Input
"+359 2 222 2222,359-2-222-2222, +359/2/222/2222, +359-2 222 2222 +359 2-222-2222,
+359-2-222-222, +359-2-222-22222 +359-2-222-2222"
Output
+359 2 222 2222, +359-2-222-2222
3. Match Dates
Write a program, which matches a date in the format "dd{separator}MMM{separator}yyyy".
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 3 of 5
Match ALL of these Match NONE of these
13/Jul/1928, 10-Nov- 01/Jan-1951, 23/sept/1973, 1/Feb/2016
1934, 25.Dec.1937
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:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 4 of 5
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.
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
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 5 of 5