reg->regexp

This commit is contained in:
Ilya Kantor 2019-09-06 16:50:41 +03:00
parent 4232a53219
commit 32e20fc97c
35 changed files with 132 additions and 132 deletions

View file

@ -9,13 +9,13 @@ Now let's show that the match should capture all the text: start at the beginnin
Finally:
```js run
let reg = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
let regexp = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
alert( reg.test('01:32:54:67:89:AB') ); // true
alert( regexp.test('01:32:54:67:89:AB') ); // true
alert( reg.test('0132546789AB') ); // false (no colons)
alert( regexp.test('0132546789AB') ); // false (no colons)
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6)
alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, need 6)
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
```

View file

@ -8,13 +8,13 @@ Write a regexp that checks whether a string is MAC-address.
Usage:
```js
let reg = /your regexp/;
let regexp = /your regexp/;
alert( reg.test('01:32:54:67:89:AB') ); // true
alert( regexp.test('01:32:54:67:89:AB') ); // true
alert( reg.test('0132546789AB') ); // false (no colons)
alert( regexp.test('0132546789AB') ); // false (no colons)
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
```

View file

@ -9,19 +9,19 @@ Here the pattern `pattern:[a-f0-9]{3}` is enclosed in parentheses to apply the q
In action:
```js run
let reg = /#([a-f0-9]{3}){1,2}/gi;
let regexp = /#([a-f0-9]{3}){1,2}/gi;
let str = "color: #3f3; background-color: #AA00ef; and: #abcd";
alert( str.match(reg) ); // #3f3 #AA00ef #abc
alert( str.match(regexp) ); // #3f3 #AA00ef #abc
```
There's a minor problem here: the pattern found `match:#abc` in `subject:#abcd`. To prevent that we can add `pattern:\b` to the end:
```js run
let reg = /#([a-f0-9]{3}){1,2}\b/gi;
let regexp = /#([a-f0-9]{3}){1,2}\b/gi;
let str = "color: #3f3; background-color: #AA00ef; and: #abcd";
alert( str.match(reg) ); // #3f3 #AA00ef
alert( str.match(regexp) ); // #3f3 #AA00ef
```

View file

@ -4,11 +4,11 @@ Write a RegExp that matches colors in the format `#abc` or `#abcdef`. That is: `
Usage example:
```js
let reg = /your regexp/g;
let regexp = /your regexp/g;
let str = "color: #3f3; background-color: #AA00ef; and: #abcd";
alert( str.match(reg) ); // #3f3 #AA00ef
alert( str.match(regexp) ); // #3f3 #AA00ef
```
P.S. This should be exactly 3 or 6 hex digits. Values with 4 digits, such as `#abcd`, should not match.

View file

@ -3,9 +3,9 @@ A positive number with an optional decimal part is (per previous task): `pattern
Let's add the optional `pattern:-` in the beginning:
```js run
let reg = /-?\d+(\.\d+)?/g;
let regexp = /-?\d+(\.\d+)?/g;
let str = "-1.5 0 2 -123.4.";
alert( str.match(reg) ); // -1.5, 0, 2, -123.4
alert( str.match(regexp) ); // -1.5, 0, 2, -123.4
```

View file

@ -5,9 +5,9 @@ Write a regexp that looks for all decimal numbers including integer ones, with t
An example of use:
```js
let reg = /your regexp/g;
let regexp = /your regexp/g;
let str = "-1.5 0 2 -123.4.";
alert( str.match(reg) ); // -1.5, 0, 2, -123.4
alert( str.match(regexp) ); // -1.5, 0, 2, -123.4
```

View file

@ -18,9 +18,9 @@ To make each of these parts a separate element of the result array, let's enclos
In action:
```js run
let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
let regexp = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
alert( "1.2 + 12".match(reg) );
alert( "1.2 + 12".match(regexp) );
```
The result includes:
@ -42,9 +42,9 @@ The final solution:
```js run
function parse(expr) {
let reg = /(-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?)/;
let regexp = /(-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?)/;
let result = expr.match(reg);
let result = expr.match(regexp);
if (!result) return [];
result.shift();

View file

@ -56,9 +56,9 @@ The email format is: `name@domain`. Any word can be the name, hyphens and dots a
The pattern:
```js run
let reg = /[-.\w]+@([\w-]+\.)+[\w-]+/g;
let regexp = /[-.\w]+@([\w-]+\.)+[\w-]+/g;
alert("my@mail.com @ his@site.com.uk".match(reg)); // my@mail.com, his@site.com.uk
alert("my@mail.com @ his@site.com.uk".match(regexp)); // my@mail.com, his@site.com.uk
```
That regexp is not perfect, but mostly works and helps to fix accidental mistypes. The only truly reliable check for an email can only be done by sending a letter.
@ -110,9 +110,9 @@ In action:
```js run
let str = '<span class="my">';
let reg = /<(([a-z]+)\s*([^>]*))>/;
let regexp = /<(([a-z]+)\s*([^>]*))>/;
let result = str.match(reg);
let result = str.match(regexp);
alert(result[0]); // <span class="my">
alert(result[1]); // span class="my"
alert(result[2]); // span
@ -336,10 +336,10 @@ let str = "Gogogo John!";
*!*
// ?: exludes 'go' from capturing
let reg = /(?:go)+ (\w+)/i;
let regexp = /(?:go)+ (\w+)/i;
*/!*
let result = str.match(reg);
let result = str.match(regexp);
alert( result[0] ); // Gogogo John (full match)
alert( result[1] ); // John