This commit is contained in:
Ilya Kantor 2019-08-21 12:32:58 +03:00
parent 047ebc8bb8
commit 3ed1024ae7
2 changed files with 13 additions and 6 deletions

View file

@ -1,10 +1,16 @@
# Patterns and flags
Regular expressions is a powerful way to search and replace in text.
In JavaScript, they are available as `RegExp` object, and also integrated in methods of strings.
## Regular Expressions
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
There are two syntaxes to create a regular expression object.
The long syntax:
The "long" syntax:
```js
regexp = new RegExp("pattern", "flags");

View file

@ -89,9 +89,11 @@ When regular expression engine is doing the search, it's moving along the string
When the pattern contains `pattern:\b`, it tests that the position in string is a word boundary, that is one of three variants:
- Immediately before is `\w`, and immediately after -- not `\w`, or vise versa.
- At string start, and the first string character is `\w`.
- At string end, and the last string character is `\w`.
There are three different positions that qualify as word boundaries:
- At string start, if the first string character is a word character `\w`.
- Between two characters in the string, where one is a word character `\w` and the other is not.
- At string end, if the last string character is a word character `\w`.
For instance, in the string `subject:Hello, Java!` the following positions match `\b`:
@ -101,11 +103,10 @@ So it matches `pattern:\bHello\b`, because:
1. At the beginning of the string the first `\b` test matches.
2. Then the word `Hello` matches.
3. Then `\b` matches, as we're between `o` and a space.
3. Then `\b` matches, as we're between `o` (a word character) and a space (not a word character).
Pattern `pattern:\bJava\b` also matches. But not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character, so there's no word boundary after it).
```js run
alert( "Hello, Java!".match(/\bHello\b/) ); // Hello
alert( "Hello, Java!".match(/\bJava\b/) ); // Java