<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Квантификаторы +, *, ? и {n} в регулярных выражениях в JavaScript</title>
</head>
<body>
<script>
var testString = "Мне не 27, а 456789 лет";
document.write('<b>Количество {n}:</b><br><br>');
matchArray = testString.match(/\d{4}/i);
document.write('<b>а) точное количество:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/\\d{4}/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(/\d{3,5}/i);
document.write('<b>б) диапазон:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/\\d{3,5}/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(/\d{3,}/gi);
document.write('<b>в) диапазон:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/\\d{3,}/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 = "+7 (905) 123-45-67";
document.write('<b>Короткие обозначения:</b><br><br>');
matchArray = testString.match(/\d+/g);
document.write('<b>а) один или более:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/\\d+/g)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br>');
testString = "Color or colour?";
matchArray = testString.match(/colou?r/ig);
document.write('<b>б) ноль или один:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/colou?r/ig)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br>');
testString = "100 10 1";
matchArray = testString.match(/\d0*/g);
document.write('<b>б) ноль или более:</b><br><br>');
document.write('Строка: «'+testString+'»<br>');
document.write('Синтаксис: testString.match(/\\d0*/g)<br>');
document.write('Совпадений: '+matchArray.length+'<br>');
for(i = 0; i < matchArray.length; i++) document.write((i+1)+'-е совпадение: «'+matchArray[i]+'»<br>');
document.write('<br>');
</script>
</body>
</html>
Результат выполнения задания можно посмотреть
здесь.