<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Скобочные группы в регулярных выражениях в JavaScript</title>
</head>
<body>
<script>
var testString = "https://egorchernorukov.ru/";
matchArray = testString.match(/^([^:]+):.*$/i);
document.write('<b>Примеры:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/^([^:]+):.*$/i)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br>');
matchArray = testString.match(/^([^:]+)(:.*)$/i);
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/^([^:]+)(:.*)$/i)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br><br>');
testString = "<span class=\"mySpan\">";
matchArray = testString.match(/^<([a-z0-9]+)\s([a-z0-9]+)=["']([a-z0-9]+)["']>$/i);
document.write('<b>Вложенные группы:</b><br><br>');
document.write('Строка: «<span class="mySpan">»<br>');
document.write('Синтаксис: testString.match(/^<([a-z0-9]+)\\s([a-z0-9]+)=[\"\']([a-z0-9]+)[\"\']>$/i)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++)
{
matchArray[i] = matchArray[i].replace(/</, '<');
matchArray[i] = matchArray[i].replace(/>/, '>');
document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
}
document.write('<br><br>');
testString = "acd";
matchArray = testString.match(/a(b)?(c)?(d)?/);
document.write('<b>Необязательные группы:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/a(b)?(c)?(d)?/)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br><br>');
testString = "<b>Текст</b>";
var matchObject = testString.matchAll(/<(\/?[a-z0-9]+)>/gi);
var matchArray = Array.from(matchObject);
var coincidenceNumber = 1;
document.write('<b>Поиск всех совпадений с группами (matchAll):</b><br><br>');
document.write('Строка: «<b>Текст</b>»<br>');
document.write('Синтаксис: testString.matchAll(/<(\\/?[a-z0-9]+)>/gi)<br>');
for(i = 0; i < matchArray.length; i++)
{
currentArrayElement = matchArray[i];
currentArrayElement.forEach(function(item, j, currentArrayElement)
{
item = item.replace(/</, '<');
item = item.replace(/>/, '>');
document.write(coincidenceNumber+'-е совпадение: «'+item+'»<br>');
});
}
document.write('<br><br>');
testString = "05-12-1970";
var matchArray = testString.match(/(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})/);
document.write('<b>Именованные группы:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: matchArray = testString.match(/(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})/)<br>');
document.write('matchArray.groups.day: '+matchArray.groups.day+'<br>');
document.write('matchArray.groups.month: '+matchArray.groups.month+'<br>');
document.write('matchArray.groups.year: '+matchArray.groups.year+'<br>');
document.write('<br><br>');
testString = "1970-12-05, 1936-03-05";
resultString = testString.replace(/(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g, "$<day>.$<month>.$<year>");
document.write('<b>Скобочные группы при замене:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.replace(/(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g, "$<day>.$<month>.$<year>")<br>');
document.write('Результат: '+resultString+'<br>');
document.write('<br><br>');
testString = "Joe Joe Black";
matchArray = testString.match(/(?:Joe\s)+(\w+)/);
document.write('<b>Исключение из запоминания:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/(?:Joe\\s)+(\\w+)/)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br><br>');
</script>
</body>
</html>
Результат выполнения задания можно посмотреть
здесь.