Jquery update div content. Refresh web page content without reloading using JavaScript

Regular expressions are one of the most powerful data manipulation tools in JavaScript. In addition, this is the most convenient way to process data in cases where it is impossible to specify an exact search pattern. The simplest example is to highlight all email addresses and phone numbers in the text. Obviously, a simple search for a substring will not work, because We do not know in advance which addresses and telephone numbers will appear in the text.

Regular expressions do not operate on exact values, but on patterns. A pattern is written using the regular expression language, and we will now get acquainted with its basics.

So, any regular expression consists of a separator, a template body and modifiers (sometimes they are also called switches).

/pattern/switch

Here "/" is the separator, "pattern" is the body of the pattern, and "switch" is the modifiers.

There are only two ways to use regular expressions in JavaScript: using the methods of the RegExp object or using the methods of the String object.

An instance of a RegExp object can be created in two ways - explicit and indirect:

// explicit method var re = new RegExp("pattern", "switch"); // indirect (alternative) method var re = /pattern/switch;

Please note that with the indirect method, the pattern is specified WITHOUT quotes.

Any regular expression consists of regular characters, special characters and repetition quantifiers. Common characters include letters, numbers, and symbols, but national characters and Unicode characters can be used as characters. Attention! "Space" is also considered significant in regular expressions, so be very careful when adding spaces to your expression. Better yet, use a special metacharacter (see the \s symbol in the table below).

Special characters (or metacharacters) are the basis of regular expressions, because It is the special characters that determine the search order and indicate to the expression interpreter additional conditions and restrictions. The table shows the main special characters and gives a brief description of them.

Special character Match in pattern
\ For characters that are usually interpreted literally, means that the next character is special. For example, /n/ matches the letter n, and /\n/ matches the newline character. For characters that are usually treated as special, means that the character must be taken literally. For example, /^/ represents the beginning of a line, while /\^/ simply matches the ^ character. /\\/ matches the backslash \.
^ Matches the beginning of a line.
$ Matches the end of the string.
(pattern) Matches the string pattern and remembers the match found.
(?:pattern) Matches the string pattern, but does not remember the match found. Used to group parts of a pattern, for example, /ko(?:t|shka)/ is a short form of the expression /cat|cat/.
(?=pattern) Lookahead matching occurs when the string pattern is matched without remembering the match found. For example, /Windows (?=95|98|NT|2000)/ matches "Windows" in the string "Windows 98", but does not match in the string "Windows 3.1". After matching, the search continues from the position next to the match found, without looking ahead.
(?!pattern) Lookahead matching occurs when the pattern string is not matched without remembering the match found. For example, /Windows (?!95|98|NT|2000)/ matches "Windows" in the string "Windows 3.1", but does not match in the string "Windows 98". After matching, the search continues from the position next to the match found, without looking ahead.
x|y Matches x or y.
Matches any character enclosed in square brackets.
[^xyz] Matches any character except those enclosed in square brackets.
Matches any character in the specified range.
[^a-z] Matches any character except those in the specified range.
\b Matches a word boundary, that is, the position between a word and a space or line feed.
\B Matches any position except a word boundary.
\cX Corresponds to the Ctrl+X symbol. For example, /\cI/ is equivalent to /\t/
\d Corresponds to the figure. Equivalent.
\D Matches a non-numeric character. Equivalent to [^0-9].
\f Corresponds to the format translation (FF) character.
\n Matches the line feed (LF) character.
\r Corresponds to the carriage return (CR) character.
\s Matches the space character. Equivalent to /[ \f\n\r\t\v]/.
\S Matches any non-whitespace character. Equivalent to /[^ \f\n\r\t\v]/.
\t Matches the tab character (HT).
\v Corresponds to the vertical tab character (VT).
\w Matches a Latin letter, number or underscore. Equivalent to / /.
\W Matches any character other than a letter, number, or underscore. Equivalent to /[^A-Za-z0-9_] /.
\n n is a positive number. Corresponds to the nth remembered substring. Calculated by counting the left parentheses. If there are fewer left parentheses before this character than n, then \0n is equivalent.
\0n n is an octal number not greater than 377. Corresponds to the character with octal code n. For example, /\011/ is equivalent to /\t/.
\xn n is a hexadecimal number consisting of two digits. Matches the character with hexadecimal code n. For example, /\x31/ is equivalent to /1/.
\un n is a hexadecimal number consisting of four digits. Corresponds to the Unicode character with hexadecimal code n. For example, /\u00A9/ is equivalent to /©/.
. Dot. Matches any character.

There are a lot of special characters and with their help you can describe almost any phrase or search phrase.

The second component of the pattern is quantifiers. This is a subset of special characters that sets the condition for the repetition of characters or their groups. The quantifier is written to the right of the expression and extends to the nearest character or group. For example:

(pattern_1)|(pattern_2)(quont)

Here the quantifier "quont" refers only to "pattern_2".

The table shows all the main quantifiers:

Quantifier Match in pattern
* Matches repeating the previous character zero or more times.
+ Matches repeating the previous character one or more times.
? Matches repeating the previous character zero or one time. . Matches any character except newline.
(n) n is a non-negative number. Matches exactly n occurrences of the previous character.
(n,) n is a non-negative number. Matches n or more occurrences of the previous character. /x(1,)/ is equivalent to /x+/. /x(0,)/ is equivalent to /x*/.
(n,m) n and m are non-negative numbers. Matches at least n and at most m occurrences of the previous character. /x(0,1)/ is equivalent to /x?/.

So, we have become familiar with the basics of the regular expression language and now we will try out our new knowledge in practice. Here is an example of using the test() method

var sample = "Oranges are made into orange juice"; var re = /orange*/; var result = re.test(sample) ? "" " : "" Not "; document.write("The string "" + sample + result + "matches the pattern " + re);

Example of using exec() method

var sample = "Oranges are made into orange juice"; var re = /orange*/; var result = re.exec(sample); document.write("Match found: "" + result + """);

And in this example we will use an alternative way to create a RegExp object:

var sample = "Oranges are made into orange juice"; var result = /orange*/.test(sample) ? "" " : "" Not "; document.write("The string "" + sample + result + "matches the sample ");

The following example is a real working script. It allows you to get rid of the bug with displaying the transparency of PNG images in Internet Explorer versions 5 and 6. We have already mentioned this script when considering layers and their use in JavaScript.

Function correctPNG() ( // check the user's browser signature string if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) ( for(var i=0; i