This commit is contained in:
Ilya Kantor 2017-03-19 17:09:37 +03:00
parent e2443e8de6
commit 75e30539ef
73 changed files with 195 additions and 195 deletions

View file

@ -2,9 +2,9 @@
Regular expressions is a powerful way of searching and replacing inside a string.
In Javascript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings.
In JavaScript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings.
Please note that regular expressions vary between programming languages. In this tutorial we concentrate on Javascript. Of course there's a lot in common, but they are a somewhat different in Perl, Ruby, PHP etc.
Please note that regular expressions vary between programming languages. In this tutorial we concentrate on JavaScript. Of course there's a lot in common, but they are a somewhat different in Perl, Ruby, PHP etc.
[cut]
@ -27,7 +27,7 @@ regexp = /pattern/; // no flags флагов
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
```
Slashes `"/"` tell Javascript that we are creating a regular expression. They play the same role as quotes for strings.
Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
## Usage
@ -36,7 +36,7 @@ To search inside a string, we can use method [search](mdn:js/String/search).
Here's an example:
```js run
let str = "I love Javascript!"; // will search here
let str = "I love JavaScript!"; // will search here
let regexp = /love/;
alert( str.search(regexp) ); // 2
@ -47,7 +47,7 @@ The `str.search` method looks for the pattern `pattern:/love/` and returns the p
The code above is the same as:
```js run
let str = "I love Javascript!"; // will search here
let str = "I love JavaScript!"; // will search here
let substr = 'love';
alert( str.search(substr) ); // 2
@ -78,7 +78,7 @@ let search = prompt("What you want to search?", "love");
let regexp = new RegExp(search);
// find whatever the user wants
alert( "I love Javascript".search(regexp));
alert( "I love JavaScript".search(regexp));
```
````
@ -87,7 +87,7 @@ alert( "I love Javascript".search(regexp));
Regular expressions may have flags that affect the search.
There are only 5 of them in Javascript:
There are only 5 of them in JavaScript:
`i`
: With this flag the search is case-insensitive: no difference between `А` and `а` (see the example below).
@ -113,7 +113,7 @@ The simplest flag is `i`.
An example with it:
```js run
let str = "I love Javascript!";
let str = "I love JavaScript!";
alert( str.search(/LOVE/) ); // -1 (not found)
alert( str.search(/LOVE/i) ); // 2

View file

@ -53,17 +53,17 @@ The array may have more than one element.
For instance:
```js run
lar str = "Javascript is a programming language";
lar str = "JavaScript is a programming language";
let result = str.match( *!*/JAVA(SCRIPT)/i*/!* );
alert( result[0] ); // Javascript (the whole match)
alert( result[0] ); // JavaScript (the whole match)
alert( result[1] ); // script (the part of the match that corresponds to the brackets)
alert( result.index ); // 0
alert( result.input ); // Javascript is a programming language
alert( result.input ); // JavaScript is a programming language
```
Due to the `i` flag the search is case-insensitive, so it finds `match:Javascript`. The part of the match that corresponds to `pattern:SCRIPT` becomes a separate array item.
Due to the `i` flag the search is case-insensitive, so it finds `match:JavaScript`. The part of the match that corresponds to `pattern:SCRIPT` becomes a separate array item.
We'll be back to brackets later in the chapter [todo]. They are great for search-and-replace.
@ -243,7 +243,7 @@ The `test` method looks for any match and returns `true/false` whether he found
So it's basically the same as `str.search(reg) != -1`, for instance:
```js run
let str = "I love Javascript";
let str = "I love JavaScript";
// these two tests do the same
alert( *!*/love/i*/!*.test(str) ); // true
@ -281,7 +281,7 @@ But the `g` flag allows to get all matches with their positions and bracket grou
Here's the example how subsequent `regexp.exec` calls return matches one by one:
```js run
let str = "A lot about Javascript at https://javascript.info";
let str = "A lot about JavaScript at https://javascript.info";
let regexp = /JAVA(SCRIPT)/ig;
@ -289,7 +289,7 @@ let regexp = /JAVA(SCRIPT)/ig;
// Look for the first match
*/!*
let matchOne = regexp.exec(str);
alert( matchOne[0] ); // Javascript
alert( matchOne[0] ); // JavaScript
alert( matchOne[1] ); // script
alert( matchOne.index ); // 12 (the position of the match)
alert( matchOne.input ); // the same as str
@ -321,7 +321,7 @@ As we can see, each `regexp.exec` call returns the match in a "full format": as
The main use case for `regexp.exec` is to find all matches in a loop:
```js run
let str = 'A lot about Javascript at https://javascript.info';
let str = 'A lot about JavaScript at https://javascript.info';
let regexp = /javascript/ig;
@ -338,7 +338,7 @@ The loop continues until `regexp.exec` returns `null` that means "no more matche
We can force `regexp.exec` to start searching from the given position by setting `lastIndex` manually:
```js run
let str = 'A lot about Javascript at https://javascript.info';
let str = 'A lot about JavaScript at https://javascript.info';
let regexp = /javascript/ig;
regexp.lastIndex = 30;

View file

@ -74,11 +74,11 @@ The word boundary `pattern:\b` -- is a special character class.
It does not denote a character, but rather a boundary between characters.
For instance, `pattern:\bJava\b` matches `match:Java` in the string `subject:Hello, Java!`, but not in the script `subject:Hello, Javascript!`.
For instance, `pattern:\bJava\b` matches `match:Java` in the string `subject:Hello, Java!`, but not in the script `subject:Hello, JavaScript!`.
```js run
alert( "Hello, Java!".match(/\bJava\b/) ); // Java
alert( "Hello, Javascript!".match(/\bJava\b/) ); // null
alert( "Hello, JavaScript!".match(/\bJava\b/) ); // null
```
The boundary has "zero width" in a sense that usually a character class means a character in the result (like a wordly or a digit), but not in this case.
@ -108,7 +108,7 @@ alert( "Hello, Java!".match(/\Java!\b/) ); // null
Once again let's note that `pattern:\b` makes the searching engine to test for the boundary, so that `pattern:Java\b` finds `match:Java` only when followed by a word boundary, but it does not add a letter to the result.
Usually we use `\b` to find standalone English words. So that if we want `"Java"` language then `pattern:\bJava\b` finds exactly a standalone word and ignores it when it's a part of `"Javascript"`.
Usually we use `\b` to find standalone English words. So that if we want `"Java"` language then `pattern:\bJava\b` finds exactly a standalone word and ignores it when it's a part of `"JavaScript"`.
Another example: a regexp `pattern:\b\d\d\b` looks for standalone two-digit numbers. In other words, it requires that before and after `pattern:\d\d` must be a symbol different from `\w` (or beginning/end of the string).

View file

@ -35,7 +35,7 @@ alert( "1\2".match(/\\/) ); // '\'
## A slash
The slash symbol `'/'` is not a special character, but in Javascript it is used to open and close the regexp: `pattern:/...pattern.../`, so we should escape it too.
The slash symbol `'/'` is not a special character, but in JavaScript it is used to open and close the regexp: `pattern:/...pattern.../`, so we should escape it too.
Here's what a search for a slash `'/'` looks like:

View file

@ -23,7 +23,7 @@ So characters like `a` and `≈` occupy 2 bytes, and those rare ones take 4.
The unicode is made in such a way that the 4-byte characters only have a meaning as a whole.
In the past Javascript did not know about that, and many string methods still have problems. For instance, `length` thinks that here are two characters:
In the past JavaScript did not know about that, and many string methods still have problems. For instance, `length` thinks that here are two characters:
```js run
alert('😄'.length); // 2

View file

@ -114,7 +114,7 @@ Regexp "open HTML-tag without attributes" (improved): `pattern:/<[a-z][a-z0-9]*>
```
Regexp "opening or closing HTML-tag without attributes": `pattern:/<\/?[a-z][a-z0-9]*>/i`
: We added an optional slash `pattern:/?` before the tag. Had to escape it with a backslash, otherwise Javascript would think it is the pattern end.
: We added an optional slash `pattern:/?` before the tag. Had to escape it with a backslash, otherwise JavaScript would think it is the pattern end.
```js run
alert( "<h1>Hi!</h1>".match(/<\/?[a-z][a-z0-9]*>/gi) ); // <h1>, </h1>