Javascript. Regular expressions. Part two

Home » Tutorials » JavaScript » Javascript. Regular expressions. Part two
In last lesson we began to learn regular expressions in javascript. In this lesson we’ll consider RegExp object functions (functions, which we considered in last lesson, belonged to String object). Also we will consider syntax of regular expressions.
There are considered functions:

  1. test function returns true or false based on the result.
  2. exec is addition for match and search functions.
    In the lesson we’ll consider the case when the flag g adds to regular expression. In this case you must know some nuances with of this flag.

Moreover syntax of regular expressions was considered.

Code lesson

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
<script>

	
	var str = "Продолжаем изучать род регулярные выражения";
	var pattern = /род/g;

	console.log(pattern.lastIndex); // 0
	console.log(pattern.test(str));
	console.log(pattern.lastIndex); // 4
	console.log(pattern.exec(str));
	console.log(pattern.lastIndex); // 22
	console.log(pattern.exec(str));
	console.log(pattern.lastIndex);	// 0

	// Синтаксис

	/*
	** \d - любая цифра (digit)
	** \s - пробел, табуляция (space)
	** \w - слово, буква(лат.) + "_" (word)
	** \b - граница (HTML5, HTML 5).
	*/

	var patternDigit = /\d/g; // поиск цифры
	var patternStr = /HTML\d/ // поиск строки HTML с цифрой после нее (HTML5)

	/*
	** Инверторы
	** \D - не цифра
	** \S - не пробел
	** \W - не слово (сюда подходят русские буквы)
	*/

	var patternPoint = /./ //поиск любого символа (кроме перевода строки \n)

	/*
	** Специальные символы - . () / \ и др. 
	*/

	/*
	** [] - поиск в наборе одного из символов
	** [-] - поиск в диапазоне - [0-9]
	** - ^ - кроме
	** + - один символ и более
	**/


	var nameStr = "@mail test@mail.ru mail.ru abzalov90@gmail.com";

	console.log(nameStr.match(/[a-zA-Z_0-9]+@[a-z]+\.\w+/g));
</script>

</body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This