the Internet Windows Android

Regular expressions. Regular Expressions (Regexp Object) Form Check Regular JavaScript Expressions Examples

Regular expressions - This is a language that describes row patterns based on metasimymbols. Metasimwall is a symbol in regular expression, which describes some class character character class, indicates the position of the substring, indicates the number of repetitions or group characters in a substring. For example, the metacimol \\ D describes the numbers, and $ denotes the end of the string. In regular expression, there may be conventional characters that describe themselves. The set and value of metasimvols in regular expressions describes the PCRE standard, most of which is supported in JS.

Regular expression scope

Regular expressions are used, as a rule, for the following tasks:

  • Comparison. The purpose of this task will find out whether the specified regular regular expression corresponds to the defined text.
  • Search. With the help of regular expressions, it is convenient to find the appropriate substring and extract them from the text.
  • Replacement. Regular expressions often help not only find, but also replace in the text of a substring corresponding to regular expression.

Ultimately, with regular expressions, you can, for example:

  • Check that custom data fill in the form.
  • Find in the user's input text link to the image for the subsequent automatic attachment to the message.
  • Remove from the text of HTML tags.
  • Check the code before compiling the presence of simple syntactic errors.

Features of regular expressions in JS. Literals of regular expressions

The main feature of regular expressions in JS is that there is a separate type of literal for them. Just as string literals are framed by quotes, regular expression literals are framed by slashes (/). Thus, the JS code may contain the expressions of the form:

console.log (Typeof / Tcoder /); // Object.

In fact, a regular expression that is determined in the string

var pattern \u003d new regexp ("tcoder");

This creation method is commonly used when it is necessary to use variables in regular expression, or create a regular expression dynamically. In all other cases, the literals of regular expressions are used due to the shorter syntax and the absence of the need to additionally shield some characters.

Symbols in regular expressions

All alphanumeric characters in regular expressions are not metasimvol and describe themselves. This means that regular expression / tcoder / It will correspond to a substring Tcoder. In regular expressions, it is also possible to indicate not alphabetic characters, such as: the translation of the string (\\ n), tabulation (\\ t) and so on. All these characters also correspond to themselves. The inverse slash (\\) delivered before the alphabetic symbol will make it a metachamir if there is such. For example, the alphabetic symbol "D" will become a metachamir describing the numbers if it is predicted with a layer (\\ d).

Classes of symbols

Single characters in regular expressions can be grouped into classes using square brackets. The class created in this way corresponds to any of the characters included in it. For example, regular expression // Letters "T", "C", "O", "D", "E", "R" will correspond.

In class, you can also set the range of characters using a hyphen. For example, the class corresponds to the class. Note that some metasimwalls in regular expressions already describe the classes of characters. For example, the metacimol \\ D is equivalent to the class. Note that the metacharacters describing the classes of characters can also be included in classes. For example, the class [\\ Da-F] corresponds to the numbers and the letters "A", "B", "D", "E", "F", that is, any hexadecimal symbol.

There is also the ability to describe the character class, specifying characters that should not enter it. This is done using metasimvol ^. For example, the class [^ \\ d] will correspond to any character except the number.

Repeat

Now we can describe, say, the decimal number of any given length, simply by writing so much metasimvolol \\ d, how many numbers in this number. Agree that this approach is not very convenient. In addition, we cannot describe the range of the required number of repetitions. For example, we cannot describe the number from one or two digits. Fortunately, in regular expressions, it is possible to describe the repetition ranges using metasimvols. To do this, after the symbol, it is enough to specify the repetition range in curly brackets. For example, regular expression / TCO (1, 3) der / The strings "Tcoder", "TCOODER" and "TCOOODER" will correspond. If you omit the maximum number of repetitions, leaving the comma and the minimum number of repetitions, then you can specify the number of repetitions more specified. For example, regular expression / Bo (2,) BS / The strings "Boobs", "BoOobs", "BooOOBS" will correspond and so on with any number of letters "O" at least two.

If in curly brackets omit and comma, simply specifying one number, then it will denote the exact number of repetitions. For example, regular expression / \\ d (5) / Five-digit numbers correspond to.

Some repetition ranges are used quite often and for their designations there are their metaches.

Greedy repetitions

The above syntax describes the maximum number of repetitions, that is, of all possible amounts of repetitions, the number of which lies in the specified range - the maximum is selected. Such repetitions are called greedy. This means that regular expression / \\ d + / in the line Yeah !! 111 will correspond to subtents "111", and not "11" or "1", although the metasimmol "+" describes the same repetitions.

If you want to implement a unclear repetition, then there is a minimum possible number of repetitions from the specified range, then simply put the symbol "?" After the repetition range. For example, regular expression / \\ D +? / In the Row "Yeah !! 111" will correspond to the substring "1", and regular expression / \\ d (2,) / In the same line will correspond to the substring "11".

It is worth paying attention to an important feature of a unclear repetition. Consider regular expression / bo (2,)? BS /. In the "I Like Big BooOOBS" line, it will fit, as with a greedy repetition, the beooobs substring, and not boobs, as it was possible to think. The fact is that a regular expression with one comparison cannot correspond to a few substring located in different places of the string. That is, our regular expression cannot match the "Boo" and "BS" substring glued into one line.

Alternatives

In regular expressions, alternatives can also be used - to describe a set of rows that corresponds to either one or another part of a regular expression. Such parts are called alternatives and are divided by vertical feature. For example, regular expression / Two | Twice | \\ 2 / It may correspond to either the "Two" substitution, or the "Twice" substring or the substring "2". The alternative chain is processed to the left to the right before the first coincidence and it can only correspond to the substring that only one alternative describes. For example, regular expression / java | script / The "I Like JavaScript" line will only match the "Java" substring.

Grouping

To consider several characters as a single integer when using the repetition ranges, symbol classes, and everything else, it is enough just to take them into parentheses. For example, regular expression / TRUE (Coder)? / The strings "TrueCoder" and "True" will correspond.

Links

In addition, the round brackets combine the symbols in regular expression into a single whole, you can refer to the appropriate substring, simply by pointing the number of the left bracket from the pair of its brackets. Brackets are numbered from left to right from one. For example, in regular terms / (One (TWO) (Four) / \\ 1 refers to one, \\ 2 on "two", \\ 3 on the "Three", \\ 4 on "Four". As an example of using such links, we give a regular expression / (\\ d) \\ 1 /which corresponds two-digit numbers with the same numbers. An important limitation of the use of backlinks is the impossibility of using their use in classes, that is, for example, describe a two-digit number with different numbers regular expression / (\\ d) [^ \\ 1] / It is impossible.

Unmasking brackets

Often it is necessary to simply group the characters, but there is no need to create a link. In this case, you can immediately write after the left grouping bracket ?:. For example, in regular terms / (One) (?: TWO) (Three) / \\ 2 will point to Three.

Such brackets are sometimes called non-prompt. They have another important feature that we will talk about in the next lesson.

Note position

In regular expressions, there are also metasimwills that indicate some position in the line. More often than all the remaining symbols ^, $ indicating the beginning and end of the line. For example, regular expression /\..+$/ will correspond to expansion in the names of files, and regular expression / ^ \\ d / The first digit in the string, if there is.

Positive and negative advanced checks

With regular expressions, you can also describe the substring, followed by or the substring described by another template. For example, we need to find the word java only if it follows "script". This task can be solved with regular expression / java (? \u003d script) /. If we need to describe the "Java" substring behind which the Script should not be used by regular expression / java (?! script) /.

We collect everything that we spoke above in one plate.

Symbol Value
a | B. Corresponds either or, or.
(…) Grouping brackets. Also on the substring, the appropriate pattern in brackets can be referenced.
(?:…) Only grouping, without the opportunity to refer.
\\ N. Reference to a substring corresponding to the N-m-template.
^ The beginning of the input data or the beginning of the string.
$ The end of the input data or the end of the string.
a (? \u003d b) Complies with the substring that describes the template A only if it follows the substring described by the template b.
a (?! B) Corresponds to the substring that describes the template A only if it not It follows the substring described by the template b.

Flags

And finally, the last element of the syntax of regular expressions. Flags set the rules of conformity that belong to the entire regular expression. In contrast to all other elements, the syntax of regular expressions they are written immediately after the literal expression, or are transmitted in the line as the second parameter of the object constructor Regexp..

JavaScript exists only three flags of regular expressions:

i. - When specifying this flag, the register is not taken into account, that is, for example, regular expression \\ javascript \\ i The strings "JavaScript", "javascript", "javascript", "javascript", etc. will be configured, etc.

m. - This flag includes a multi-line search. This means that if in the text there are character translation symbols and this flag is delivered, the characters ^ and $ besides the beginning and the end of the whole text will also correspond to the beginning and the end of each row in the text. For example, regular expression / Line $ / m Complies with the "line" substring, both in the "First Line" line and in the One \\ Nsecond Line \\ NTWO string.

g. - Includes a global search, that is, a regular expression, if this flag is enabled, the substring coincided with it will correspond, and not just the first, as if this flag is not.

Flags can be combined in an arbitrary order, that is \\ Tcoder \\ Mig, \\ TCODER \\ Gim, \\ Tocder \\ GMI etc., this is the same. The order of flags does not matter if they are transmitted in the string as the second argument the object constructor Regexp., i.e nEW REGEXP ("TCODER", "IM") and nEW REGEXP ("TCODER", "IM") Also one thing.

ZY

Regular expressions are a very powerful and convenient tool for working with rows, allowing to reduce hundreds of code strings to one expression. Unfortunately, their syntax is sometimes too complicated and hard and even the most experienced developer may forget that he meant a rather complicated regular expression, which he wrote a couple of days ago, if he did not comment. For these reasons, sometimes it is still worth abandoning regular expressions in favor of conventional methods for working with lines.

Regular expressions ( Regexp.) - This is a very effective way to work with rows.

Mapping a regular expression using a special syntax you can:

  • search text in line
  • replace substring in line
  • remove information From string

Almost all programming languages \u200b\u200bhave regular expressions. There are small differences in implementation, but general concepts are applied almost everywhere.

Regular expressions refer to the 1950s when they were formalized as a conceptual search template for string processing algorithms.

Regular expressions implemented in UNIX, such as grep, SED and popular text editors, began to gain popularity and have been added to the PERL programming language, and later in many other languages.

JavaScript, along with Perl, this is one of the programming languages \u200b\u200bin which the support of regular expressions is built directly in the tongue.

Difficult, useful

Newbies regular expressions may seem absolute nonsense, and often even professional developers, if not to invest it is necessary for their understanding.

Regular expressions it's hard to write, it is difficult to read and difficult to maintain / change.

But sometimes regular expressions are the only reasonable way Perform some manipulations above the rows, so they are a very valuable tool.

This guide is aimed at the easiest way to give you some idea of \u200b\u200bregular expressions in JavaScript and provide information on how to read and create regular expressions.

The empirical rule is that simple regular expressions are simple for reading and writing., while complex regular expressions can quickly turn into a messIf you do not deeply understand the basics.

What do regular expressions look like

In JavaScript, regular expressions are an object that can be defined in two ways.

The first way is to create new Regexp object With the help of a designer:

Const Re1 \u003d New Regexp ("Hey")

The second way is to use literals of regular expressions:

Const RE1 \u003d / Hey /

You know what's in javascript there literal objects and literals Massives? In it also have regexp Literals.

In the above example, Hey is called template. In the literal form, it is between two slashes, and in the case of the designer of the object, no.

This is the first important difference between the two ways to define regular expressions, the rest we will see later.

How do they work?

Regular expression that we have determined above as RE1 is very simple. It searches for a hey string without any limitations: the string may contain a lot of text, and the word Hey is somewhere in the middle and regular expression will work. The string may contain only the word Hey and regularly will work again.

It is quite simple.

You can try to test the regular expression using the Regexp.Test (String) method that returns the logical (Boolean) value:

RE1.Test ("Hey") // ✅ RE1.Test ("BLABLABLA Hey Blablabla") // ✅ RE1.Test ("He") // ❌ RE1.Test ("BLABLABLA") // ❌

In the example above, we simply checked if the "Hey" pattern of a regular expression, which is stored in RE1.

It is easier than simple, but you already know a lot about regular expressions.

Fixing

/ Hey /

it will work independently of where hey is inside the string.

If you want to find lines that start with Hey, then use the operator ^:

/^hey/.test ("hey ") // ✅ / ^hey/.test ("bla hey") // ❌

If you want to find rows that end on Hey, then use the operator $:

/hey$/.test ("hey ") // ✅ /hey$/.test ("bla hey") // ✅ /hey$/.test("hey you ") // ❌

By combining the two previous operators, you can find a string that completely coincides with Hey:

/^Hey$/.test("hey ") // ✅

To find a string starting with one substring, and you can use an ending confirmation. *, Which will coincide with any symbol repeating 0 or more times:

/^Hey.*joe$/.test ("hey joe ") // ✅ / ^HEY.*Joe$/.test ("heyjoe") // ✅ /^Hey.*joe$/.test("hey How Are you joe ") // ✅ /^Hey.*joe$/.test("hey joe!") // ❌

Search elements by range

Instead of looking for a specific string, you can specify the range of characters, for example:

// // A, B, C, ..., X, Y, Z // // A, B, C, ..., X, Y, Z // // A, B, C // / / 0, 1, 2, 3, ..., 8, 9

These regular expressions are looking for lines that contain at least one character from the selected range:

//.test("a ") // ✅ //.test("1") // ❌ //.test("a ") // ❌ //.test("d") // ❌ // .test ("DC") // ✅

Ranges can be combined:

// //.test("a ") // ✅ //.test("1") // ✅ //.test("a ") // ✅

Search for multiple coincidences of the range element

You can check whether the string contains only one character from the range using the symbol -:

/ ^ $ / / ^$/.test("a ") // ✅ /^$/.test ("ab") // ❌

Inversion of template

The symbol ^ At the beginning of the template binds it to the beginning of the line.

Using this symbol inside the range inverts the range, so:

/[I-za-z0-9 ]/.test("a ") // ❌ / [И ада-za-z0-9 ]/.test ("1") // ❌ / [^ A-ZA -Z0-9] /. Test ("A") // ❌ / [И ада-za-z0-9 ]/.test("@ ") // ✅

Metacters

  • \\ D coincides with any number equivalent
  • \\ D coincides with any symbol that is not a number equivalent to [^ 0-9]
  • \\ w coincides with any alphanumeric symbol equivalent
  • \\ W coincides with any symbol that is not an alphanumeric value equivalent to [^ a-za-z0-9]
  • \\ s coincides with any space icon: space, tab, symbol of a new row and spaces Unicode
  • \\ S coincides with any symbol that is not a blank
  • \\ 0 coincides with NULL
  • \\ n coincides with a new line symbol
  • \\ t coincides with the tab symbol
  • \\ uxxxx coincides with the Unicode symbol with XXXX code (U flag required)
  • . Coincides with any symbol, except for a new string symbol (such as \\ n) (if you do not use the flag s, explain later)
  • [^] coincides with any symbol, including a new string symbol. Useful when working with multi-line strings

Selection in regular expressions

If you want to choose one or other string, use the operator | .

/hey|ho/.test("hey ") // ✅ /hey|ho/.test("ho") // ✅

Quantifier

Imagine that you have a regular expression that checks the string to consist only from one digit:

you can use quantifier ? which will make this symbol optional. In our case, the figure should occur 0 or 1 time:

but what if we want the regular expression to have a few digit?

You can do it in 4 ways using +, *, (n) and (n, m).

+

Coincides with one or more (\u003e \u003d 1) elements:

/ ^ \\ D + $ / / / / d+$/.Test("12) // ✅ /^ \\d+$/.test("14) // ✅ /^ \\d+$/.test("144343 " ) // ✅ / ^ \\ d + $ /. Test ("") // ❌ /^ \\d+$/.test ("1a ") // ❌

*

Coincides with 0 or more (\u003e \u003d 0) elements:

/ ^ \\ d + $ / / ^ \\ / \\ / Тест ("12) // ✅ /^ \\d.7/.test("14 ") // ✅ / ^ \\ d * $ /. Test ( "144343") // ✅ / ^ \\ d * $ /. Test ("") // ✅ /^ /D.7/.test("1 ") // ❌

(N)

The coincides exactly with n number of elements:

/ ^ \\ d (3) $ / / / /d(3)$/.test("123 ") // ✅ /^ \\d(3)$/.test("12") // ❌ / ^ \\ (N, M)

Coincides with the range from n to M elements:

/ ^ \\ D (3.5) $ / / ^ \\d(3,5)$/.test ("123 ") // ✅ /^ \\d(3,5)$/.test("21234") // ✅ /^ \\d(3,5)$ /.test("12345 ") // ✅ /^ \\d(3,5)$/.test("123456") // ❌

m can be omitted and leave the second limit without restrictions so that there is a minimum of N elements:

/ ^ \\ d (3,) $ / / ^ \\d(3 ,)$/.test ("2) // ❌ /^ \\d(3 ,)$/.test("123) // ✅ /^ \\d(3.12345 ") // ✅ /^ \\d(3 ,)$/.test("123456789") // ✅

Optional elements

Next Sign Element? , make it optional:

/ ^ \\ D (3) \\ w? $ / /^ \\d(3) \\w? $//.test("123) // ✅ / ^ \\ D (3) \\ W? $ /. Test (" 123A ") // ✅ /^ \\d(3) \\w? $/.test("123ab") // ❌

Groups

Using parentheses, you can create symbol groups (...).

The example below is looking for an exact coincidence of 3 digits followed by one or more alphanumeric characters:

/ ^ (\\ d (3)) (\\ w +) $ / /) (\\d(3)) (\\w+ )$/.test("123) // ❌ / ^ (\\ d (3)) ( \\ w +) $ // test ("123s") // ✅ /^( \\d(3)) (\\w+ )$/.test ("223something ") // ✅ / ^ (\\ d (3)) ( \\ w +) $ /. Test ("1234") // ✅

Repeating characters that are found after the closing group of parentheses belong to the entire group:

/ ^ (\\ d (2)) + $ / ^( \\d(2 ))+$/.test("12) // ✅ / ^ (\\ d (2)) + $ /. Test (" 123 ") // ❌ /^( \\d(2) )+$/.test("1234") // ✅

Capture group

So far we have seen how to test lines and check whether they contain a specific template.

The cool possibility of regular expressions is that capture certain parts of the line and put them in an array.

You can do it with the help of groups, or rather with capture Group.

By default, groups are captured. Now instead of using Regexp.Test (String), which simply returns a logical value, we will use one of the following methods:

  • String.Match (Regexp)
  • Regexp.exec (String)

They are absolutely the same and both return an array with a conducted string as the first element, and in the other elements of the coincidence for each group found.

If no coincidences found, it returns NULL.

"123S" .match (/ ^ (\\ d (3)) (\\ w +) $ /) // Array ["123s", "123", "123s"] / ^ (\\ d (3)) (\\ W + ) $ /. exec ("123S") // Array ["123s", "123", "s"] "hey" .match (/ (Hey | HO) /) // Array ["Hey", "Hey "] /(hey|ho)/.exec ("hey") // Array ["Hey", "Hey"] /(hey|ho )/.exec ("ha! ") // null

When the group matches several times, then only the last value will be added to the returned array.

"123456789" .match (/ (\\ d) + /) // Array ["123456789", "9"]

Optional groups

Capture groups can be made optional with (...)? . If nothing is found, the element undefined will be added to the returned array:

/^ (\\D (3)))) () (\\W+ )$/.exec ("23 s ") // array [" 123 s "," 123 "," "," s "] / ^ (\\ d (3)) (\\ s)? (\\ w +) $ /. exec ("123S") // Array ["123s", "123", undefined, "s"]

Link to the group found

Each found group is assigned a number. $ 1 refers to the first element, $ 2 to the second, and so on. This is useful when we will talk about replacing part of the line.

Named capture group

This is a new ES2018 feature.

The group can be assigned a name, and not just a slot in the return array:

Const Re \u003d / (? \\ D (4)) - (? \\ D (2)) - (? \\ D (2)) / const Result \u003d Re.exec ("2015-01-02") // result.groups.year \u003d\u003d\u003d "2015"; // result.groups.month \u003d\u003d\u003d "01"; // result.groups.day \u003d\u003d\u003d "02";

Using Match and Exec without groups

There is a difference when using Match and Exec without groups: In the first element, the array will not be fully found line, but a direct coincidence:

/hey|ho/.exec("hey ") // [" Hey "] /(hey).(ho )/.exec ("hey ho") // ["Hey Ho", "Hey", "Ho "]

Lipped groups

Since the default groups are captured, we need a way to ignore some groups in the returned array. This is possible with scorched groupswho start with (?: ...).

"123S" .match (/ ^ (\\ d (3)) (?: \\ S) (\\ w +) $ /) // null "123 s" .match (/ ^ (\\ d (3)) (?: \\ s) (\\ w +) $ /) // array ["123 s", "123", "s"]

Flags

You can use the following flags on any regular expressions:

  • g: looking for coincidences globally
  • i: makes a regular expression not sensitive to register
  • m: Includes multi-line mode. In this mode, ^ and $ coincide with the beginning and end of the entire row. Without this flag, with multi-line strings, they coincide with the beginning and end of each row.
  • u: Includes Unicode support (Added to ES6 / ES2015)
  • s: (New in ES2018) Reduction from "Single Line", it allows. Coincident with the symbols of a new line

Flags can be combined, as well as they are added to the end of the literal line:

/hey/ig.test("hey ") // ✅

or are transmitted by the second parameter in the Regexp object constructor:

NEW REGEXP ("Hey", "Ig"). Test ("Hey") // ✅

Inspection of regular expressions

You can inspect the properties of regular expressions:

  • source - Line of Template
  • multiline - TRUE is taken if the M flag is set
  • global - True value is accepted if the G flag is set
  • ignorecase - the value is TRUE if the flag i is set
  • lastindex
/^ (\\w(3))$/i.Source // "^ (\\\\ D (3)) (\\\\ w +) $" /^ (\\w(3))$/i.multiline // False /^( \\w(3))$/i.lastindex // 0 / ^( \\w(3))$/i.Ignorecase // true / d (\\w(3))$/i.global // false

Shielding

Special symbols:

These are special characters because they are control characters when drawing up regular expressions, so if you want to use them to search for coincidences inside the template, then you need to shield them using a symbol of the reverse slash:

/ ^ \\\\ $ / / ^ \\ ^ $ / // / ^ \\ ^ Test ("^ ") ✅ / ^ \\ $$ / // / ^$/.test ("$") ✅

Borders of string

\\ b and \\ b allow you to determine whether the string is in the beginning or end of the word:

  • \\ B coincides if the set of characters is at the beginning or end of the word
  • \\ B coincides if the set of characters is not at the beginning or end of the word

"I Saw a Bear" .match (/ \\ BBEAR /) // Array ["Bear"] "I Saw a Beard" .match (/ \\ BBEAR /) // Array ["Bear"] "I Saw a Beard" .match (/ \\ BBEAR \\ B /) // NULL "Cool_Bear" .match (/ \\ BBEAR \\ B /) // NULL

Replacement with regular expressions

We have already seen how to check the lines to match the template.

We also seen how to extract part of the rows corresponding to the template into an array.

Now let's look at how replace part of the line Based on the template.

The String object in JavaScript has a REPLACE () method that can be used without regular expressions for one replacement in line:

"Hello World!". Replace ("WORLD", "DOG") // Hello Dog! "My Dog Is a Good Dog!". Replace ("Dog", "Cat") // My Cat IS A Good Dog!

This method can also receive a regular expression as an argument:

"Hello World!" Replace (/ world /, "Dog") // Hello Dog!

Using the flag G is - this the only way Replace several occurrences in the row on Vanilla JavaScript:

"My Dog Is a Good Dog!". Replace (/ Dog / G, "Cat") // My Cat IS A Good Cat!

Groups allow us to make more bizarre things, change parts of the lines:

"Hello, World!". Replace (/ (\\ w +), (\\ w +)! /, "$ 2: $ 1 !!!") // "World: Hello !!!"

Instead of a string, you can use the function to make even more interesting things. It will be transferred to a number of arguments, such as the String.Match (Regexp) or Regexp.exec (String) methods return, where the number of arguments depends on the number of groups:

"Hello, World!". Replace (/ (\\ w +), (\\ w +)! /, (Matchedstring, First, Second) \u003d\u003e (Console.log (FIRST); Console.log (Second); Return `$ ( second.touppercase ()): $ (first) !!! `)) //" WORLD: Hello !!! "

Greed

Regular expressions are called greedy default.

What does it mean?

We take for example is a regular expression:

/\\$ (.+) \\s?/

It is assumed that we need to extract the amount in dollars from the line:

/\\$(.+)'S?/.exec ("this costs $ 100 ") // 0

but what if we have more words after the number, it distracts

/\\$ (.+)) \\s?/.exec ("this costs $ 100 and it is less than $ 200 ") // 100 and it is less than $ 200

Why? Because the regular expression after the $ sign coincides with any symbol. + And does not stop until it reaches the end of the line. Then he stops, because \\ s? makes the final space optional.

To fix it, we need to specify that the regular expression should be lazy And find the smallest number of coincidences. Can we do this with the symbol? After quantifier:

/\\$(.+ ?) /S/.exec ("this costs $ 100 and it is less than $ 200) // 100

So, the symbol? may mean different things depending on its position, so it can be a quantifier and indicator lazy mode.

Ahead: matching string depending on what it follows

Using? \u003d To search for coincidences in a string for which a defined substring follows

/ Roger (? \u003d Waters) / / Roger (? \u003d Waters) /. Test ("Roger IS My Dog") // false / Roger (? \u003d Waters) /. Test ("Roger Is My Dog and Roger Waters Is A Famous Musician ") // True

Performs a reverse operation and finds coincidences in the string for which not A definite substring follows:

/ Roger (?! Waters) / / Roger (?! Waters) /. Test ("Roger Is My Dog") // True / Roger (?! Waters) /. Test ("Roger IS My Dog and Roger Waters Is a FAMOUS MUSICIAN ") // FALSE

Retrospective: matching string depending on what precedes it

This is a new ES2018 feature.

Ahead uses a symbol? \u003d. Retrospective uses?<= :

/(?<=Roger) Waters/ /(?<=Roger) Waters/.test("Pink Waters is my dog") //false /(?<=Roger) Waters/.test("Roger is my dog and Roger Waters is a famous musician") //true

Inversion of retrospectives use?

/(?

Regular expressions and Unicode

The U flag is mandatory when working with unicode strings, in particular when it may be necessary to process lines in astral planes that are not included in the first 1600 Unicode characters.

For example, Emodi, but only they.

/^.$/.test("a ") // ✅ / ^.$ /. Test ("? ") // ❌ / ^.$/ U.Test ("? ") // ✅

Therefore, always use the U flag.

Unicode, as well as ordinary characters, can process the ranges:

//.test("a ") // ✅ //.test("1") // ✅ / [? -?] / u.Test ("?") // ✅ / [? -?] / U .test ("?") // ❌

JavaScript checks internal presentation codes, so?< ? < ? на самом деле \u1F436 < \u1F43A < \u1F98A . Посмотрите полный список эмодзи чтобы увидеть коды и узнать их порядок.

Screening Properties Unicode.

As we said above, in a regular expression template you can use \\ D to find a coincidence on any digit, \\ S to find a match on any character besides a space, \\ W to find a coincidence on any alphanumeric symbol, etc.

The unicode properties shielding is the possibility of ES2018, which adds a very cool function, expanding this concept on all unicode characters and adding \\ p () and \\ p ().

Any unicode symbol has a set of properties. For example, Script defines the Languages \u200b\u200bFamily, ASCII is a logical value equal to True for ASCII characters, etc. You can put this property in curly brackets and the regular expression will verify that its value was true:

/^ /u_ascii)+$ /U.Test("abc ") // ✅ / ^ \\ p (ASCII) + $ / u.Test (" [Email Protected]") // ✅ /^ \\p(scii )+$ /u.test ("abc?") // ❌

ASCII_HEX_DIGIT is another logical property that checks whether the string contains only valid high-alkali numbers:

/^ /p(Scii_Hex_Digit )+$/u.Test("0123456789Abcdef ") // ✅ /^ \\p_ascii_hex_digit )+$/u.test( "h")

There are many other logic properties that you can check by simply adding their name into curly brackets, including UpperCase, LowerCase, White_Space, Alphabetic, Emoji and others:

/^ /u.Test("h ") // ✅ /^\\p(uppercase )$ /U.Test("h") // ✅ / ^ \\ p (emoji) + $ / U.Test ("H") // ❌ / ^ \\ p (Emoji) + $ / u.Test ("??") // ✅

In addition to these binary properties, you can check any property of the Unicode symbol to correspond to a specific value. In the example below, I check whether the string in the Greek or Latin alphabet is recorded:

/^ Cro_Script\u003dGreek)+$/u.Test("ελληνικά ") // ✅ /^ \\p(script\u003dlatin) +$/u.Test("hey") // ✅

Examples

Extraction of a number from a string

Suppose there is a string containing only one number that needs to be removed. / \\ d + / should do it:

"Test 123123329" .match (/ \\ d + /) // Array ["123123329"]

Search E-mail Addresses:

The simplest approach is to check the unbelievable characters before and after the @ sign, with the help of \\ s:

/( \\S+ )@ (\\S+) \\ ((\\S+ )/ / (\\ s +) @ (\\ s +) \\. (\\ S +) /. Exec (" [Email Protected]") //["[Email Protected]"," Copesc "," Gmail "," COM "]

However, this is a simplified example, since there are many not valid e-mail addresses under it.

Capture text between double quotes

Imagine that you have a string that contains text prisoner in double quotes and you need to extract this text.

The best way to do it is to use capture group, Because we know that our coincidence should begin and end with the symbol ", so we can easily configure the template, but we also want to remove these quotes from the result.

We will find what we need in RESULT:

Const Hello \u003d "Hello" Nice Flower "" const result \u003d /" ([_""* "/.EEXEC(Hello) // Array ["\\" Nice Flower \\ "", "Nice Flower"]

Obtaining content from HTML tag

For example, get the contents from the span tag, allowing any number of arguments for the tag:

/]*>(.*?)<\/span>/ /]*>(.*?)<\/span>/.exec ("test ") // NULL / ]*>(.*?)<\/span>/.exec ("test ") // [" Test "," Test "] / ]*>(.*?)<\/span>/.exec (" test") // ["test"," test "]

The REGEXP class in JavaScript is a regular expression - an object describing the character template. Regexp objects are usually created using the special syntax of the literals below, but can also be created by the constructor REGEXP ().

Syntax

// Using a special literal syntax var Regex \u003d / Pattern / Flags; // With the designer var Regex \u003d New Regexp ("Pattern", "Flags"); var Regex \u003d New Regexp (/ Pattern /, "Flags");

Parameter values:

Flags of regular expressions

FlagDescription
g. Allows you to find all the coincidences, and not stop after the first coincidence ( global Match Flag.).
i.Allows you to compare without registering ( ignore Case Flag).
m.Mapping is made in several lines. Processing of the initial and final characters (^ and $) is made according to several lines, that is, the mapping occurs with the beginning or end of each row (\\ N or \\ r dividers), and not only with the beginning, or the end of the entire line ( multiline Flag.).
u.The template will be regarded as a sequence of unicode code points ( unicode Flag.).
y.The mapping occurs in the index to which indicates the property of the LastINDEX of this regular expression, while the mapping is not made in later, or early index ( sticky Flag.).

Symbol sets

Metacters

SymbolDescription
. Allows you to find one character, except for the symbol of a new string, or the end of the string (\\ n, \\ r, \\ u2028 or \\ u2029).
\\ D.Allows you to find a symbol of numbers in the basic Latin alphabet. Equivalentine use character set.
\\ D.Allows you to find any character that is not a digit in the basic Latin alphabet. Equivalent to a set of characters [^ 0-9].
\\ S.Allows you to find a single blank symbol. Under the space symbol it is understood as a space, tabulation, page translation, the translation of the string and other globular symbols of UNICOD. Equivalent to a set of characters [\\ f \\ n \\ r \\ t \\ v \\ u00a0 \\ u1680 \\ u180e \\ u2000 \\ u2001 \\ u2002 \\ u2003 \\ u2004 \\ u2005 \\ u2006 \\ u2007 \\ u2008 \\ u2009 \\ u2008 \\ u2009 \\ u2008 \\ u2028 \\ u2029 \\ u202f \\ u205f \\ u3000].
\\ S.Allows you to find a single symbol that is not a blank. Under the space symbol it is understood as a space, tabulation, page translation, the translation of the string and other globular symbols of UNICOD. Equivalent to a set of characters [^ \\ f \\ n \\ r \\ t \\ v \\ u00a0 \\ u1680 \\ u180e \\ u2000 \\ u2001 \\ u2002 \\ u2003 \\ u2004 \\ u2005 \\ u2006 \\ u2007 \\ u2008 \\ u2009 \\ u2008 \\ u2009 \\ u2008 \\ U2028 \\ u2029 \\ u202f \\ u205f \\ u3000].
[\\ b]Allows you to find the Backspace symbol (special symbol \\ B, U + 0008).
\0 Allows you to find a symbol 0 (zero).
\\ N.Allows you to find a new string symbol.
\\ F.Allows you to find a page translation symbol.
\\ R.Allows you to find a carriage return symbol.
\\ T.Allows you to find a symbol of the horizontal tab.
\\ V.Allows you to find a symbol of the vertical tab.
\\ W.Allows you to find any alphanumeric symbol of the base Latin alphabet, including underline. Equivalent to a set of characters.
\\ W.Allows you to find any character that is not a symbol from the base Latin alphabet. Equivalent to a set of characters [^ a-za-z0-9_].
\\ CX.Allows you to find a control symbol in the string. Where X is the letter from A to Z. For example, / \\ cm / denotes the Ctrl-M symbol.
\\ Xhh.Allows you to find a symbol using hexadecimal value (HH is a two-digit hexadecimal value).
\\ uhhhh.Allows you to find a symbol using UTF-16 encoding (HHHH - four-digit hexadecimal value).
\\ U (hhhh) or
\\ U (hhhhh)
Allows you to find a symbol with the value of Unicode U + HHHH or U + Hhhhh (hexadecimal). Only when the U flag is specified.
\ Indicates that the next character is special and should not be interpreted literally. For symbols, which are usually interpreted in a special way, indicates that the next character is not special and must be interpreted literally.

Restrictions

Quantifier

SymbolDescription
n *Movement occurs with any string containing zero or more occurrences of the symbol n..
n +.Mowing occurs with any string containing at least one character n..
n?Movement occurs with any string with a previous element n. Zero or once.
n (x)Corresponds to any row containing a sequence of characters n. A certain number of times x.. X.
n (x,) x. Embodiments of the previous element n.. X. There must be a whole positive number.
n (x, y)Corresponds to any row containing at least x.but not more than with y. entries of the preceding element n.. X. and y. There must be integer positive numbers.
n *?
N +?
N ??
n (x)?
n (x,)?
n (x, y)?
Movement occurs by analogy with quantifiers *, + ,? and (...) However, while the search goes the minimum possible comparison. The default is "greedy" mode ,? At the end of the quantifier allows you to specify the "unjone" mode at which the repetition of the comparison occurs the minimum possible number of times.
x (? \u003d y)Allows you to compare x.only if for x. follow y..
x (?! Y)Allows you to compare x.only if for x. do not do it y..
x | y.Comparison occurs with any of the indicated alternatives.

Grouping and backlinks

SymbolDescription
(x)Allows you to find a symbol x. and remember the result of the comparison ("exciting brackets"). A compared substring can be caused from the elements of the resulting array ..., [n], or from the properties of the predefined Regexp object of $ 1 ..., $ 9.
(?: X)Allows you to find a symbol x.But not to memorize the result of the comparison ("non-missing brackets"). A compiled substring cannot be caused from the elements of the resulting array ..., [n], or from the properties of the predefined Regexp object $ 1 ..., $ 9.
\\ N.The opposite link to the last substitute, which coincides with the N-o, in the account in parentheses in regular terms (the numbering of the brackets goes left to right). n. There must be a whole positive number.

Regular expressions Allow a flexible search for words and expressions in texts to remove them, extract or replace them.

Syntax:

// First option for creating a regular expression VAR Regexp \u003d New Regexp ( template,modifiers); // Second version of the creation of a regular expression var Regexp \u003d / template/modifiers;

template Allows you to set the character template for search.

modifiers allow you to configure search behavior:

  • i. - Search without registration of the case of letters;
  • g. - Global Search (all coincidences will be found in the document, and not just the first);
  • m. - Multi-line search.

Search for words and expressions

The simplest use of regular expressions is to find words and expressions in various texts.

Let us give an example of using the search using modifiers:

// Set the regular expression RV1 RV1 \u003d / Russia /; // Set the regular expression RV2 RV2 \u003d / Russia / G; // Set the regular expression RV3 RV3 \u003d / Russia / Ig; // A bold font is allocated, where coincidences will be found in the text when using // Expressions RV1: Russia is the world's largest state. Russia borders with 18 countries. Russia is the state-continuer of the USSR. // A bold font is allocated, where coincidences will be found in the text when used // Expressions RV2: Russia is the world's largest state. Russia borders with 18 countries. Russia is the state-continuer of the USSR. "; // Fat font allocated, where coincidences will be found in the text when using // Expressions RV3: Russia is the largest state of the world. Russia borders with the 18 countries. Russia is the state - the successor of the USSR.";

Special symbols

In addition to conventional characters in regular expression templates can be used special symbols (Metacters). Special characters with descriptions are shown in the table below:

Special symbol Description
. Coincides with any symbol, except the string end symbol.
\\ W. Coincides with any alphabetic symbol.
\\ W. Coincides with any not alphabetic symbol.
\\ D. Coincides with the characters that are numbers.
\\ D. Coincides with characters that are not numbers.
\\ S. Coincides with the blade symbols.
\\ S. Coincides with not space symbols.
\\ B. The coincidences will be searched only at the boundaries of the words (at the beginning or end).
\\ B. The coincidences will be distorted only not at the boundaries of words.
\\ N. Coincides with a row translation symbol.

/ * Expression REG1 will find all the words beginning with two arbitrary letters and ending with "Vet". Since the words in the proposal are divided by a space, then at the beginning and at the end we will add specialsimol \\ s) * / reg1 \u003d / \\ s..t. \\ s / g; txt \u003d "hello covenant velvetet closet"; Document.Write (TXT.Match (REG1) + "
"); / * Expression REG2 will find all words that begin with three arbitrary letters and ending with" Vet "* / reg2 \u003d / \\ s ... Womb \\ s / g; document.write (txt.match (REG2) +"
"); TXT1 \u003d" ON PLAYER OH HIGH PREMIRT "; / * Expression REG3 will find all words that begin on" with "in which then follows 1 digit and end on" Vet "* / var reg3 \u003d / with \\ DVL / G; Document .write (TXT1.Match (REG3) + "
"); // Expression REG4 will find all the numbers in the text var reg4 \u003d / \\ d / g; txt2 \u003d" 5 years of study, 3 years of swimming, 9 years old shooting. "Document.Write (TXT2.Match (REG4) +"
");

Quick view

Symbols in square brackets

Using square brackets [Kayu] You can specify a group of characters that you need to produce.

Symbol ^ in front of a group of characters in square brackets [^ qd] It suggests that it is necessary to search for all the characters of the alphabet other than those specified.

Using the dash (-) between the symbols in square brackets [AA] You can set the range of characters, which you want to produce.

With the help of square brackets, you can also search for numbers.

// Set the regular expression REG1 REG1 \u003d / \\ SCO [TDM] \\ S / G; // Set the string of the text TXT1 TXT1 \u003d "Cat Spit Code Kom Kommersant"; // Produce using regular expression REG1 Search on the TXT1 Document.Write line (TXT1.Match (REG1) + "
"); REG2 \u003d / \\ SLO [^ TG] / G; TXT2 \u003d" Slot Slot Slog "; Document.Write (TXT2.Match (REG2) +"
"); REG3 \u003d // G; TXT3 \u003d" 5 years of study, 3 years of swimming, 9 years old shooting "; document.write (txt3.match (REG3));

Quick view

Quantifier

Quantifier - This design allows you to set how many times the symbol preceding it or a group of characters should meet in a coincidence.

Syntax:

// The preceding symbol should meet X - times (x) // The preceding symbol should meet from x to a while inclusive (x, y) // The preceding symbol must meet at least x times (x,) // Indicates that the preceding symbol must occur 0 or more times * // Indicates that the preceding symbol should meet 1 or more times + // Indicates that the preceding symbol must occur 0 or 1 times ?


// Set the regular expression RV1 RV1 \u003d / Co. (5) WAT / G // Set the regular expression RV2 RV2 \u003d / KO (3,) WATDER / G // Set the regular expression RV3 RV3 \u003d / Co. + WAT / G // Set Regular expression RV4 RV4 \u003d / ko? WATER / G // Set the regular expression RV5 RV5 \u003d / KO * WATDER / G // bold font shows where coincidences will be found in the text using // expressions RV1: Cushka Cat Kooshka Cat Coach Koooooh Co -oooooooooooooooooooooooo // bold font shows where the text will be found in the text when using // Expressions RV2: Cash Cat Cat kOO -OPOO COOOOOKHOP COOOOOOOOOKH COOOOOOOOHOKHA // bold font shows where coincidences will be found in the text when using // Expressions RV3: Cushka cat Kooshka Coooshop COOOOOKHOKO KOOOOOOOOPOO COOOOOOOOOOHKA // Bold font shows where the text will be found in the text when using // Expressions RV4: Cash Cat Kooshop COOOOOKHOK COOOOOOKHOKA COOOO -OOOKHOP COOOOOOHKA // Bold font shows where the text will be found in the text using // RV5 expressions: cash Cat Cat Kooshka Kooooooooo Coooooooooooooooooooooooooooooo

Note: If you want to use any special symbol (such as. * +? or ()) How the usual you must put in front of it \\.

Using round brackets

By entering part of the regular expression template into parentheses, you specify the expression to remember the coincidence, found by this part of the template. The saved coincidence can be used later in your code.

For example, the regular expression / (Dmitry) \\ Svasiliev / will find the line "Dmitry Vasilyev" and will remember the substring "Dmitry".

In the example below, we use the REPLACE () method to change the word order in the text. To appeal to the saved coincidence, we use $ 1 and $ 2.

Var Regexp \u003d / (Dmitry) \\ s (Vasilyev) /; Var Text \u003d "Dmitry Vasilyev"; var newtext \u003d text.replace (Regexp, $ 2 $ 1); Document.Write (NewText);

Quick view

Round brackets can be used to group characters before quantifier.

Some people, faced with a problem, think: "Oh, and I use regular expressions." Now they have two problems.
Jamie Runovsky

Yuan-Ma said: "It takes a lot of power to cut a tree across the structure of wood. A lot of code is required to program the collapse of the problem.
Master of Yuan-Ma, "Programming Book"

Programming tools and techniques survive and distributed by a chaotic-evolutionary way. Sometimes they survive not beautiful and ingenious, but simply those who work quite well in their field - for example, if they are integrated into another successful technology.

In this chapter, we will discuss this tool - regular expressions. This is a way to describe templates in string data. They create a small separate language that is included in JavaScript and in many other languages \u200b\u200band tools.

Regular regularities are very strange and extremely useful. Their syntax is mysterious, and the program interface in JavaScript is clumsy. But this is a powerful tool for research and row processing. Having understood with them, you will become a more efficient programmer.

Create regular expression

Regular is the type of object. It can be created by calling the REGEXP constructor, or writing the desired pattern, surrounded by stroces.

Var Re1 \u003d New Regexp ("ABC"); VAR RE2 \u003d / ABC /;

Both of these regular expressions represent one template: the "A" symbol followed by the "B" symbol followed by the "C" symbol.

If you use the REGEXP constructor, then the template is written as a regular string, so all the rules are relative to the reverse layers.

The second entry, where the template is between the slashes, processes the inverse strokes differently. First, since the pattern ends with a straight track, then you need to put a reverse lay in front of the direct slab that we want to include in our template. In addition, the inverse strokes that are not part of the special characters of the type \\ n will be saved (and not ignored, as in lines), and change the meaning of the template. Some characters, such as a question mark or a plus, is of particular importance in regular regularities, and if you need to find such a symbol, it should also be preparing the reverse track.

Var EIGHTEENPLUS \u003d / EIGHTEEN \\ + /;

To know which characters you need to prevent a slash, you need to learn a list of all special characters in regular. While it is unrealistic, so in case of doubt, just put the reverse lay in front of any symbol that is not a letter, number or space.

Check on coincidence

Regulators have several methods. Simplest - Test. If you give it a string, it will return the Boolean value, reporting whether the line contains the entry of the specified template.

Console.log (/Abc/.Test ("abcde")); // → True Console.log (/ABC/.Test ("abxde")); // → False.

Regular, consisting only of non-special characters, simply represents the sequence of these characters. If ABC is somewhere in the line that we check (not only at the beginning), Test will return True.

We are looking for a set of characters

To find out whether the ABC string contains, it would be possible with indexof. Regularly allow you to go further and make up more complex patterns.

Suppose we need to find any number. When we set the set of characters in square brackets in regular brackets, this means that this part of the expression coincides with any of the characters in brackets.

Both expressions are in the lines containing the digit.

Console.log (//. Test ("In 1992")); // → True Console.log (//. Test ("In 1992")); // → True.

In square brackets, the dash between two characters is used to specify the range of characters, where the sequence is set to Unicode encoding. Symbols from 0 to 9 are there simply in a row (codes from 48 to 57), so it captures them all and coincides with any digit.

Several characters groups have their own built-in reductions.

\\ D any digit
\\ w alphanumeric symbol
\\ s Blanket Symbol (Space, Tab, Row Translation, etc.)
\\ D not a digit
\\ W is not an alphanumeric symbol
\\ S is not a blank symbol
. Any symbol, except for the translation line

In this way, you can ask the date and time format like 30-01-2003 15:20 by the following expression:

Var datetime \u003d / \\ d \\ d- \\ d \\ d- \\ d \\ d \\ d \\ d \\ d \\ d: \\ d \\ d /; Console.log (datetime.test ("30-01-2003 15:20")); // → True Console.log (DateTime.Test ("30-Jan-2003 15:20")); // → False.

Looks terrible, right? Too many reverse layers, which make it difficult to understand the template. Later we will slightly improve it.

Reverse strokes can also be used in square brackets. For example, [\\ d] means any digit or point. Note that the point inside square brackets loses its special value and turns simply to the point. The same applies to other special characters, type +.

Invert a set of characters - that is, to say that you need to find any character, except for those in the set - you can, putting a sign ^ immediately after the opening square bracket.

Var notbinary \u003d / [^ 01] /; Console.log (notbinary.test ("1100100010100110")); // → False Console.log (notbinary.test ("1100100010200110")); // → True.

We repeat parts of the template

We know how to find one digit. And if we need to find the number of entirely - a sequence of one or more digits?

If you put after something in the regular sign +, it will mean that this element can be repeated more than once. / \\ d + / means one or more digits.

Console.log (/ "\\ d +" /. Test ("" 123 "")); // → True Console.log (/ "\\ d +" /. Test ("" "")); // → False console.log (/ "\\ d *" /. Test ("" 123 "")); // → True Console.log (/ "\\ d *" /. Test ("" "")); // → True.

In the stars * the value is almost the same, but it allows the template to attend zero times. If after something stands the stars, then it never prevents the template to find the template in the string - it is simply there is zero times.

The question mark makes part of the template optional, that is, it can meet zero or once. In the following example, the U symbol may occur, but the template coincides when it is not.

Var neighbor \u003d / neighbou? R /; Console.log (Neighbor.Test ("Neighbor")); // → True Console.log (Neighbor.Test ("Neighbor")); // → True.

To set an exact number of times that the template must meet, curly brackets are used. (4) After the element means that it should meet in line 4 times. You can also set the gap: (2.4) means that the element must meet at least 2 and not more than 4 times.

Another version of the date and time format, where days, months and hours from one or two digits are allowed. And she is just more readable.

Var datetime \u003d / \\ d (1,2) - \\ d (1,2) - \\ d (4) \\ d (1,2): \\ d (2) /; Console.log (DateTime.Test ("30-1-2003 8:45")); // → True.

You can use the intervals with an open end, lowering one of the numbers. (, 5) means that the template can meet from zero to five times, and (5,) - from five or more.

Grouping a subscription

To use * or + operators on multiple items immediately, you can use parentheses. Part of the regular, enclosed in brackets, is considered one element from the point of view of operators.

Var cartooncrying \u003d / Boo + (Hoo +) + / i; Console.log (CartoonCrying.Test ("BooHooOOHOOHOOO")); // → True.

The first and second advantages include only second letters about in the words of Boo and Hoo. The third + refers to a whole group (HOO +), finding one or more such sequences.

The letter I at the end of the expression makes the regularly insensitive to the symbol register - so that B coincides with b.

Coincidences and groups

TEST Method is the easiest method of checking the regulators. He only reports whether a coincidence was found, or not. Regulators have another EXEC method, which will return NULL if nothing has been found, and otherwise the object will return with information about the coincidence.

Var match \u003d /\\d+/.exec ("one two 100); Console.log (Match); // → ["100"] Console.log (Match.Index); // → 8.

A returned Exec object has an Index property, which contains the character number from which the coincidence happened. In general, the object looks like an array of strings, where the first element is a string that was checked into a coincidence. In our example, it will be the sequence of the figures we were looking for.

The strings have a MATCH method that works about the same.

Console.log ("One Two 100" .match (/ \\ d + /)); // → ["100"]

When regularly contains a submissions grouped by round brackets, the text that coincided with these groups will also appear in the array. The first element is always a whole coincidence. The second is part that coincided with the first group (the one who had round brackets earlier than all), then with the second group, and so on.

Var quotedText \u003d / "([^"] *) "/; console.log (quotedText.exec (" SHE SAID "Hello" ")); // → [" Hello "", "Hello"]

When the group was not found at all (for example, if the question mark is behind it), its position in the array contains undefined. If the group coincided several times, then in the array there will be only the last coincidence.

Console.log (/ Bad (LY)? /. Exec ("Bad")); // → ["Bad", undefined] Console.log (/ (/ d) + /. Exec ("123")); // → ["123", "3"]

Groups are useful for extracting part parts. If we do not just need to check if the date line is in the line, and to remove it and create an object representing the date, we can enter into a number of numbers into parentheses and select a date from Exec.

But to begin with a slight digression in which we learn the preferred way of storing the date and time in JavaScript.

Date type

JavaScript has a standard type of object for dates - or rather, moments in time. It is called Date. If you just create a date object via New, you will receive the current date and application.

Console.log (new date ()); // → Sun Nov 09 2014 00:07:57 GMT + 0300 (CET)

You can also create an object containing the specified time.

Console.log (New Date (2015, 9, 21)); // → WED OCT 21 2015 00:00:00 GMT + 0300 (CET) Console.log (New Date (2009, 11, 9, 12, 59, 59, 999)); // → Wed Dec 09 2009 12:59:59 GMT + 0300 (CET)

JavaScript uses an agreement in which numbers begin with scratch, and the number of days from the unit. It is stupid and ridiculous. Go up.

The last four arguments (hours, minutes, seconds and milliseconds) are optional, and in the absence of equal to zero.

Time labels are stored as the number of milliseconds who have passed since the early 1970s. For the time until 1970, negative numbers are used (this is due to the UNIX Time Agreement, which was created at about the time). The GetTime Method of the Date object returns this number. It is naturally big.
Console.log (New Date (2013, 11, 19) .gettime ()); // → 1387407600000 CONSOLE.LOG (New Date (1387407600000)); // → THU DEC 19 2013 00:00:00 GMT + 0100 (CET)

If you specify a DATE design one argument, it is perceived as a number of milliseconds. You can get the current Millisecond value by creating the Date object and calling the GetTime method, or by calling the Date.Now function.

The Date object to extract its components has GETFULLYEAR, GETMONTH, GetDate, Gethours, GetMinutes, and GetSeconds. There is also a GETYEAR method that returns a fairly useless two-digit code, type 93 or 14.

By concluding the necessary parts of the template into parentheses, we can create a date object directly from the string.

FUNCTION FINDDATE (STRING) (VAR DATETIME \u003d / (\\ D (1,2)) - (\\ D (1,2)) - (\\ d (4)) /; var Match \u003d DateTime.exec (string); Return New Date (Number (Match), Number (Match) - 1, Number (Match));) Console.log (Findate ("30-1-2003")); // → Thu Jan 30 2003 00:00:00 GMT + 0100 (CET)

Borders of the Word and Row

Unfortunately, FindDate also joyfully removes a meaningless date 00-1-3000 from the line "100-1-30000". The coincidence may happen anywhere in the line, so in this case it will just start from the second symbol and finished on the penultimate.

If we need to force a coincidence to take the entire line of the whole, we use tags ^ and $. ^ coincides with the beginning of the line, and $ with the end. Therefore, / ^ \\ d + $ / coincides with a string consisting of only one or more digits, / ^! / Coincides with the partner starting with an exclamation mark, and the / x ^ / does not match any line (before starting the line it cannot be x).

If, on the other hand, we just need to make sure that the date begins and ends at the edge of the word, we use the label \\ b. The word boundary may be the beginning or end of the string, or any place of the line, where on the one hand is the alphanumeric-digital symbol \\ W, and on the other - not alphanumeric.

Console.log (/cat/.Test ("Concatenate")); // → True Console.log (/ \\ BCAT \\ B / .Test ("ConcateNate")); // → False.

Note that the border label is not a symbol. It is just a limitation denoting that the coincidence occurs only if a certain condition is performed.

Templates with choices

Suppose it is necessary to find out whether the text contains not just a number, and the number followed by PIG, COW, or Chicken in a single or multiple number.

It would be possible to write three regularly and check them in turn, but there is a way better. Symbol | Indicates the choice between the templates on the left and to the right of it. And we can say the following:

Var an animalcount \u003d / \\ b \\ d + (pig | cow | chicken) s? \\ B /; Console.log (AnimalCount.test ("15 Pigs")); // → True Console.log (Animalcount.Test (15 Pigchickens)); // → False.

Brackets limit part of the template to which |, and you can put a lot of such operators with each other in order to designate the choice of more than two options.

Search mechanism

Regular expressions can be considered as flowcharts. The following diagram describes the last animal code.

The expression coincides with the string if you can find the path from the left side of the chart to the right. We remember the current position in the row, and each time, passing a rectangle, check that part of the line immediately behind our position in it coincides with the contents of the rectangle.

So, checking the coincidence of our regular regularity in the line "The 3 Pigs" when passing on the block diagram looks like this:

At position 4 there is a border of the word, and we pass the first rectangle
- Starting with 4 positions we find a digit, and we pass the second rectangle
- At position 5, one way closes back in front of the second rectangle, and the second goes back to the rectangle with a space. We have a space, not a digit, and we choose the second way.
- Now we are at position 6, the beginning of "PIGS", and on triple branching of paths. There is no "COW" or "Chicken" line, but there is a "PIG", so we choose this path.
- at position 9 after a triple branching, one way bypass "s" and heads to the last rectangle with the word boundary, and the second passes through "S". We have "s", so we go there.
- On position 10, we at the end of the line, and the coincidence can only the border of the word. The end of the line is considered the boundary, and we pass through the last rectangle. And so we successfully found our template.

In principle, regular expressions work as follows: the algorithm begins at the beginning of the line and tries to find a coincidence there. In our case, there is a border of the word, so he passes the first rectangle - but there are no numbers, so he stumbles on the second rectangle. Then he moves to the second symbol in the line, and is trying to find a coincidence there ... and so on until he finds a coincidence or does not reach the end of the line, in which case coincidence is found.

Kickbacks

Regular / \\ b (+ b | \\ d + | [\\ da-f] h) \\ b / coincides either with a binary number followed by b or with a decimal number without suffix or hexadecimal (numbers from 0 to 9 or symbols From A to H), followed by h. Relevant diagram:

In search of the coincidence, it may happen that the algorithm went along the top path (binary number), even if there is no such number in the line. If there is a string "103", for example, it is clear that only reaching the figures 3 algorithm will understand that he is on the wrong path. In general, the line coincides with the regular, just not in this branch.

Then the algorithm makes a rollback. On the fork, he remembers the current position (in our case, it is the beginning of the line, immediately after the word border) so that you can go back and try another way if the selected does not work. For a string "103" after meeting with the top three, he will return and try to pass the way for decimal numbers. It will work, so the coincidence will be found.

The algorithm stops as soon as the complete coincidence will find. This means that even if several options can approach, only one of them is used (in the order in which they appear in the regular).

Rolls occur when using repetition operators, such as + and *. If you are looking for / ^*.* / in the ABCXE string, part of the regular. * Tries to absorb the whole line. The algorithm will then figure out that he also needed "x". Since no "X" after the end of the line is not, the algorithm will try to look for a coincidence, throwing away to one character. After ABCX, there is also no X, then he again rolls back, already to the ABC substring. And after the line, he finds X and reports on a successful coincidence, in positions from 0 to 4.

You can write regularly, which will lead to multiple rollbacks. Such a problem occurs when the template may coincide with the input data set of different methods. For example, if we make a mistake when writing regularly for binary numbers, we can accidentally write something like / (+) + b /.

If the algorithm will search for such a template in a long row of nonols and units that does not contain at the end of "b", it will first pass through the inner loop until it end the numbers. Then he will notice that at the end there is no "b", will make a rollback for one position, it will take place on the outer loop, again will give up, try to roll back to another position on the inner loop ... and it will be further to look for the way, cycling both hinges. That is, the amount of work with each row symbol will double. Even for several dozen characters, the coincidence search will take a very long time.

Replace method

The strings have a Replace method that can replace part of the line of another string.

Console.log ("dad" .replace ("P", "M")); // → Mapa

The first argument can be regular, in which case is replaced by the first entry of the regular regularity in the string. When the "G" option (global, universal) is added to the Regular, all entries are replaced, and not just the first

Console.log ("Borobudur" .replace (//, "A")); // → Barobudur Console.log ("Borobudur" .replace (// G, "A")); // → Barabadar

It would make sense to transmit the "Replace All" option through a separate argument, or through a separate ReplaceAll method. But unfortunately, the option is transmitted through the regularly itself.

All power regulators are disclosed when we use references to those found in the group string set in regular. For example, we have a string containing the names of people, one name on the line, in the format "Surname, Name". If we need to change them in places and remove the comma, so that the name of the name is "name", we write the following:

Console.log ("Hopper, Grace \\ Nmccarthy, John \\ Nritchie, Dennis" .replace (/ ([\\ w] +), ([\\ w] +) / g, "$ 2 $ 1")); // → GRACE HOPPER // John McCarthy // Dennis Ritchie

$ 1 and $ 2 in the replacement line refer to groups of characters enclosed in brackets. $ 1 is replaced by the text that coincided with the first group, $ 2 - with the second group, and so on, up to $ 9. The entire coincidence is contained in the $ &.

You can also transfer both a second argument as a second argument. For each replacement, a function will be caused, the arguments of which are found groups (and the entire coincidence of the entire line), and its result will be inserted into a new string.

Simple example:

VAR S \u003d "The Cia and FBI"; Console.log (S.REPLACE (/ \\ B (FBI | CIA) \\ b / g, function (RETURN STR.ToupPerCase ();))); // → The Cia and FBI

But more interesting:

Var Stock \u003d "1 Lemon, 2 Cabbages, and 101 Eggs"; Function MinusOne (Match, Amount, Unit) (Amount \u003d Number (Amount) - 1; if (amount \u003d\u003d 1) // Only one remained, delete "S" at the end of the Unit \u003d Unit.Slice (0, Unit.Length - 1); ELSE If (amount \u003d\u003d 0) amount \u003d "no"; Return amount + "" + unit;) console.log (stock.replace (/ (\\ d +) (\\ w +) / g, minusone)); // → No Lemon, 1 Cabbage, and 100 Eggs

The code takes the string, finds all the entry of the numbers, followed by the word, and returns the line where each number is reduced by one.

The group (\\ D +) falls into the Amount argument, A (\\ W +) - in Unit. The function converts Amount to the number - and it always works, because our template is just \\ D +. And then makes changes to the Word, in case only 1 item remains.

Greed

It is easy with replace to write a function that removes all comments from the JavaScript code. Here is the first attempt:

FUNCTION STRIPCOMMENTS (Code) (Return code.replace (/ \\/ \\/** | \\ / \\ * [^] * \\ * \\ // g, "");) console.log (stripcomments ("1 + / * 2 * / 3 ")); // → 1 + 3 Console.log (stripcomments ("x \u003d 10; // Ten!")); // → x \u003d 10; Console.log (stripcomments ("1 / * A * / + / * b * / 1")); // → 1 1

Part in front of the operator "or" coincides with two slashes, followed by any number of characters, except for the symbol of the row translation. A piece that removes multi-line comments is more complex. We use [^], i.e. Any character that is not empty as a way to find any character. We cannot use the point, because block comments continue on the new line, and the line translation symbol does not match the point.

But the output of the previous example is incorrect. Why?

Part [^] * first try to capture as many characters as it can. If, because of this, the next part of the regularly does not find coincidences, it will roll back to one character and try again. In the example, the algorithm tries to capture the entire string, and then rolls back. Falling at 4 characters ago, he will find in the line * / - and this is not what we achieved. We wanted to capture only one comment, and not to go through the line and find the last comment.

Because of this, we say that repeat operators (+, *,?, And ()) greedy, that is, they first capture how much they can, and then go back. If you put a question after such an operator (+?, *?, (), ()?), They will turn into unjudgingly, and will begin to find the smallest of possible entries.

And this is what we need. Forcing the stars to find coincidences in the lowest possible number of stitch symbols, we absorb only one block of comments, and nothing more.

Function StripComments (Code) (Return code.Replace (/ \\ //.** | \\ / \\ * [^] *? \\ * \\ // g, "");) Console.log (stripcomments ("1 / * a * / + / * b * / 1 ")); // → 1 + 1

Many errors occur when using greedy operators instead of unjudging. When using the repeat operator, you first always consider the option of a unjudene operator.

Dynamic Creating Regexp Objects

In some cases, the exact template is unknown while writing code. For example, you will need to look for the username in the text, and enter it into an emphasis. Since you will learn only after starting the program, you can not use the record with the stacked.

But you can build a string and use the REGEXP constructor. Here is an example:

Var Name \u003d "Harry"; Var Text \u003d "And Harry on the forehead scar."; var Regexp \u003d New Regexp ("\\\\ B (" + Name + ") \\\\ b", "gi"); console.log (text.replace (Regexp, "$ 1_")); // → And at _Garry_ on the forehead scar.

When creating the boundaries, the word has to use double layers, because we write them in a normal line, and not in regular with straight stairs. The second argument for REGEXP contains options for regulators - in our case "Gi", i.e. Global and register-independent.

But what if the name is "dea + hlrd" (if our user is Kulkhazker)? As a result, we will get a senseless regular regularly, which will not find in the line of coincidences.

We can add backlash to any symbol that we do not like. We can not add inverse strokes in front of the letters, because \\ b or \\ n is a special mixture. But you can easily add stuck in front of any alphabetically digital symbols.

Var name \u003d "dea + hlrd"; Var Text \u003d "This Dea + Hlrd got everyone."; var escaped \u003d name.replace (/ [^ \\ w \\ s] / g, "\\\\ $ &"); var regexp \u003d new regexp ("\\\\ b (" + escaped + ") \\\\ b", "gi"); console.log (text.replace (Regexp, "$ 1_")); // → This _dea + Hlrd_ got everyone.

SEARCH method

The IndexOF method cannot be used with regular. But there is a SEARCH method that is just waiting for regular. Like IndexOF, it returns the index of the first entry, or -1, if it did not happen.

Console.log ("Word" .Search (/ s /)); // → 2 console.log (". .Search (/ s /)); // → -1.

Unfortunately, it is impossible to set the method to search for a coincidence, starting with a specific offset (as can be done with indexof). It would be useful.

Property Lastindex

Exec method either does not allow a convenient way to start searching from a given position in the string. But an uncomfortable way gives.

Object Regulators has properties. One of them is a source containing a string. Another one is Lastindex, which is controlling, in some conditions where the next search for occurrences will begin.

These conditions include the need for the presence of the global G option, and the fact that the search should go using the EXEC method. A more reasonable solution would simply be allowed to allow an additional argument to transmit to Exec, but rationality is not a fundamental feature in the javascript regulators interface.

Var Pattern \u003d / Y / G; Pattern.LastIndex \u003d 3; Var Match \u003d Pattern.exec ("xyzzy"); Console.log (Match.Index); // → 4 Console.log (Pattern.Lastindex); // → 5.

If the search was successful, the Exec call updates the Lastindex property, so that it indicates a position after the found entry. If there was no success, Lastindex is installed in zero - as well as Lastindex in the just created object.

When using the Global Variable Regular and Multiple Exec Calls, these automatic Lastindex updates can lead to problems. Your regularly can start a search from the position remaining from the previous call.

Var digit \u003d / \\ d / g; Console.log (Digit.EXEC ("HERE IT IS: 1")); // → ["1"] Console.log (Digit.exec ("and now: 1")); // → NULL

Another interesting effect of the G option is that it changes the work of the Match method. When it is called with this option, instead of returning an array, similar to the Exec work result, it finds all the entry of the template in the string and returns an array from the found substring.

Console.log ("Banana" .match (/ en / g)); // → ["An", "An"]

So carefully with global variables. In cases where they are needed - calls Replace or places where you specifically use Lastindex - perhaps all cases in which they should be applied.

Cycles by entering

A typical task is to go through all the entries of the template in the string so as to have access to the MATCH object in the cycle body using Lastindex and Exec.

Var input \u003d "Stitch with 3 numbers in it ... 42 and 88."; var number \u003d / \\ b (\\ d +) \\ b / g; Var Match; While (Match \u003d Number.exec (Input)) Console.log ("found", Match, "on", Match.Index); // → Found 3 to 14 // Found 42 to 33 // Found 88 to 40

The fact that the value assignment is assigned to the value. Using the Match \u003d Re.exec (INPUT) design as a condition in the WHILE cycle, we produce a search at the beginning of each iteration, we save the result in a variable, and end the cycle when all the coincidences are found.

Hearing ini files

In conclusion of the chapter, consider the task using the Regulators. Imagine that we write a program that collects information about our enemies via the Internet in automatic mode. (I will not write the entire program, only the part that reads the file with the settings. Sorry.) The file looks like this:

Searchengine \u003d http: //www.google.com/search? Q \u003d $ 1 SpiteFulness \u003d 9.7; Before the comments, the point is placed; Each section applies to a separate enemy FullName \u003d Larry Doe Type \u003d Bychar from kindergarten WebSite \u003d http: //www.geocities.com/capecanaveral/11451 FullName \u003d Gargamel Type \u003d Angry Wizard Outputdir \u003d / Home / Marijn / Enemies / Gargamel

The exact file format (which is quite widely used, and is usually called INI), the following:

Empty strings and rows starting with a semicolon are ignored
- Rows enclosed in square brackets start a new section
- Rows containing the alphanumeric identifier, followed by \u003d, add settings in this section.

Everything else is incorrect data.

Our task is to convert such a string into an array of objects, each with the Name property and the array of settings. For each section, you need one object, and one more - for the global settings from the top of the file.

Since the file should be disassembled line, it is not bad to start with breaking the file to the string. To do this, in chapter 6, we used string.split ("\\ n"). Some Operations are used to transfer the line not one character \\ N, and two - \\ r \\ n. Since the SPLIT method takes regularly as an argument, can we divide the line using an expression / \\ r? \\ N /, allowing and single \\ n and \\ r \\ n between rows.

Function Parseini (String) (// start from an object containing top-level settings VAR CurrentSection \u003d (name: , fields :); var certifications \u003d; string.split (/ \\ r? \\ N /). Foreach (FUNCTION (LINE ) (var Match; if (/ \\^ /S* (;;.*)? $/.test(line)) (Return;) ELSE if (Match \u003d line.match (/^\\[_.* :) $$ /)) (CurrentSection \u003d (Name: Match, Fields :); Categories.Push (CurrentSection);) ELSE if (Match \u003d line.match (/ ^ (\\ w +) \u003d (. *) $ /)) (CurrentSection. fields.push ((Name: Match, Value: Match));) ELSE (Throw New Error ("Stitch" + Line + "" contains incorrect data. ");))); Return Categories;)

The code passes all lines by updating the current section "Current Section". At first it checks whether it is possible to ignore the line, with the help of regular / \\^ (;; .*)?$/. Consider how it works? Part between the brackets coincides with the comments, eh? Does that regularly coincides with the lines consisting of some spaces.

If the string is not a comment, the code checks whether it starts a new section. If yes, it creates a new object for the current section to which subsequent settings are added.

The last meaningful feature is a row is a normal setup, and in this case it is added to the current object.

If no option has worked, the function gives an error.

Notice how frequent use ^ and $ cares about the fact that the expression coincides with the entire string of the whole, and not with a part. If not to use them, the code as a whole will work, but sometimes there will be strange results, and this error will be difficult to track.

The IF design (Match \u003d String.match (...)) is similar to a trick that uses the assignment as a condition in the WHILE cycle. Often you do not know that Match call will be successful, so you can access the resulting object only inside the IF block that checks. So as not to break a beautiful IF check chain, we assign the search result of the variable, and immediately use this assignment as a check.

International Symbols

Due to the initially simple implementation of the language, and the subsequent fixation of this implementation "in granite", JavaScript regularly stupid with symbols that do not occur in English. For example, the symbol of "letters" from the point of view of JavaScript regulators can be one of the 26 letters of the English alphabet, and for some reason even emphasis. The letters of type é or β, uniquely being lettered, do not coincide with \\ W (and coincide with \\ w, that is, with a not-letter).

According to a strange coincidence, historically \\ s (space) coincides with all the characters that are considered to be glibble, including such things as an indispensable space or a Mongolian vowel separator.

In some implementations, regulators in other languages \u200b\u200bhave a special syntax to search for special categories of Unicode characters, such as "all capital letters", "All punctuation marks" or "control characters". There are plans to add such categories in JavaScript, but they seem to be implemented soon.

Outcome

Regular regularities are objects representing search patterns in lines. They use their syntax to express these templates.

/ abc / symbol sequence
// Any symbol from the list
/ [^ ABC] / any character except characters from the list
// Any symbol of gap
/ x + / one or more occurrences of template x
/ x +? / one or more occurrence, undead
/ x * / zero or more occurrences
/ x? / zero or one occur
/ x (2.4) / from two to four occurrences
/ (ABC) / Group
/ A | B | C / any of several templates
/ \\ d / any digit
/ \\ w / any alphanumeric symbol ("letter")
/ \\ s / any blank symbol
/./ Any symbol, except for translation row
/ \\ b / word border
/ ^ / Start line
/ $ / End line

Regular has a TEST method, to check whether there is a pattern in a string. There is an Exec method that returns an array containing all the found groups. The massif has an index property, where the character number from which the coincidence has happened.

The strings have a MATCH method to search for templates, and the SEARCH method returning only the initial position of entry. The Replace method can replace the entry of the template to another string. In addition, you can pass the function in the REPLACE that will build a replacement line based on the template and found groups.

Regulators have settings that are written after a closing slash. The I option I makes a regular case-dependent, and the G option G makes it global, which, among other things, causes the Replace method to replace all the found things, and not just the first.

Regexp Designer can be used to create regular rows.

Regular - a sharp tool with an uncomfortable handle. They greatly simplify some tasks, and can become uncontrollable in solving other, complex tasks. Part of the ability to use regular regularity is to be able to resist the temptation to stuff in them the task for which they are not intended.

Exercises

Inevitably, when solving the tasks, you will have incomprehensible cases, and you can sometimes despair, seeing the unpredictable behavior of some regulators. Sometimes it helps to explore the behavior of regularly through the online service of the DEBUGGEX.COM, where you can see its visualization and compare with the desired effect.
Regular golf
"Golf" in the code is called the game where you need to express a given program with a minimum number of characters. Regular golf is a practical exercise on writing the smallest possible regulators to search for a given template, and only it.

For each of the substitches, write the regular to check them in the row. Regularly must find these specified substring. Do not worry about the boundaries of words, if it is not mentioned especially. When you get a working regular, try to reduce it.

CAR and CAT.
- POP and PROP
- Ferret, Ferry, and Ferrari
- Any word ending with iOs
- Space, followed by a dot, comma, colon or comma point.
- Word length six letters
- Word without letters E

// Enter your regular vermiy (/.../, ["My Car", "Bad Cats"], ["Camper", "High Art"]); verify (/.../, ["POP CULTURE", "MAD PROPS"], ["plop"]); Verify (/.../, ["Ferret", "Ferry", "Ferrari"], ["Ferrum", "Transfer A"]); verify (/.../, ["How Delicious", "Spacious Room"], ["Ruinous", "Consciousness"]); Verify (/.../, ["Bad Punctuation."], ["Escape the dot"]); verify (/.../, ["hottentotentialtenen"], ["no", "hotten totten tenten"]); verify (/.../, ["Red Platypus", "Wobbling Nest"], ["Earth Bed", "Learning Ape"]); FUNCTION VERIFY (REGEXP, YES, NO) (// ignore unfinished exercises if (regexp.source \u003d\u003d "...") return; Yes.Foreach (Function (s) (if (! regexp.test (s)) Console .log ("not found" "+ s +" "");)); no.Foreach (function (S) (REGEXP.Test (S)) Console.log ("unexpected entry" + s + " "");));)

Quotes in the text
Suppose you wrote a story, and everywhere for the designation of dialogues used single quotes. Now you want to replace quotes of dialogs to double, and leave single in abbreviations of the type Aren't.

Come up with a template that distinguishes two of these quotes and write a call to the Replace method that replaces.

Again numbers
The sequences of numbers can be found simple regular / \\ D + /.

Write an expression that finds only the numbers recorded in the JavaScript style. It should maintain a possible minus or plus before the number, decimal point, and exponential 5E-3 or 1E10 exponential recording - again with a possible plus or minus. Also note that before or after the point do not necessarily stand the numbers, but the number cannot consist of one point. That is, .5 or 5. - permissible numbers, and one point by itself - no.

// Enter the regular regular. var number \u003d / \u003d...$/; // Tests: ["1", "-1", "+15", "1.55", ".5", "5.", "1.3E2", "1E-4", "1E + 12"] .Foreach (Function (s) (if (! Number.test (S)) Console.log ("did not find" + s + "");)); ["1A", "+ -1", "1.2.3", "1 + 1", "1e4.5", ".5.", "1f5", "."]. Foreach (Function (S) (If (Number.test (S)) Console.log ("incorrectly accepted" "+ s +" ");));