diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/solution.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/solution.md index 91568d03..69ade1b1 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/solution.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/solution.md @@ -1,8 +1,8 @@ Answer: `pattern:\d\d[-:]\d\d`. ```js run -let reg = /\d\d[-:]\d\d/g; -alert( "Breakfast at 09:00. Dinner at 21-30".match(reg) ); // 09:00, 21-30 +let regexp = /\d\d[-:]\d\d/g; +alert( "Breakfast at 09:00. Dinner at 21-30".match(regexp) ); // 09:00, 21-30 ``` Please note that the dash `pattern:'-'` has a special meaning in square brackets, but only between other characters, not when it's in the beginning or at the end, so we don't need to escape it. diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/task.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/task.md index 868115bd..c8441caf 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/task.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/2-find-time-2-formats/task.md @@ -5,8 +5,8 @@ The time can be in the format `hours:minutes` or `hours-minutes`. Both hours and Write a regexp to find time: ```js -let reg = /your regexp/g; -alert( "Breakfast at 09:00. Dinner at 21-30".match(reg) ); // 09:00, 21-30 +let regexp = /your regexp/g; +alert( "Breakfast at 09:00. Dinner at 21-30".match(regexp) ); // 09:00, 21-30 ``` P.S. In this task we assume that the time is always correct, there's no need to filter out bad strings like "45:67". Later we'll deal with that too. diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md index 6beca62b..cb6a27e9 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md @@ -130,18 +130,18 @@ In the example below the regexp `pattern:[-().^+]` looks for one of the characte ```js run // No need to escape -let reg = /[-().^+]/g; +let regexp = /[-().^+]/g; -alert( "1 + 2 - 3".match(reg) ); // Matches +, - +alert( "1 + 2 - 3".match(regexp) ); // Matches +, - ``` ...But if you decide to escape them "just in case", then there would be no harm: ```js run // Escaped everything -let reg = /[\-\(\)\.\^\+]/g; +let regexp = /[\-\(\)\.\^\+]/g; -alert( "1 + 2 - 3".match(reg) ); // also works: +, - +alert( "1 + 2 - 3".match(regexp) ); // also works: +, - ``` ## Ranges and flag "u" diff --git a/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/solution.md b/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/solution.md index d4ddb136..21b8762e 100644 --- a/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/solution.md +++ b/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/solution.md @@ -2,8 +2,8 @@ Solution: ```js run -let reg = /\.{3,}/g; -alert( "Hello!... How goes?.....".match(reg) ); // ..., ..... +let regexp = /\.{3,}/g; +alert( "Hello!... How goes?.....".match(regexp) ); // ..., ..... ``` Please note that the dot is a special character, so we have to escape it and insert as `\.`. diff --git a/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/task.md b/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/task.md index 6fd91bdc..4140b4a9 100644 --- a/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/task.md +++ b/9-regular-expressions/09-regexp-quantifiers/1-find-text-manydots/task.md @@ -9,6 +9,6 @@ Create a regexp to find ellipsis: 3 (or more?) dots in a row. Check it: ```js -let reg = /your regexp/g; -alert( "Hello!... How goes?.....".match(reg) ); // ..., ..... +let regexp = /your regexp/g; +alert( "Hello!... How goes?.....".match(regexp) ); // ..., ..... ``` diff --git a/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/solution.md b/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/solution.md index d4d297a1..b9e1f85a 100644 --- a/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/solution.md +++ b/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/solution.md @@ -7,11 +7,11 @@ Then we can look for 6 of them using the quantifier `pattern:{6}`. As a result, we have the regexp: `pattern:/#[a-f0-9]{6}/gi`. ```js run -let reg = /#[a-f0-9]{6}/gi; +let regexp = /#[a-f0-9]{6}/gi; let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2" -alert( str.match(reg) ); // #121212,#AA00ef +alert( str.match(regexp) ); // #121212,#AA00ef ``` The problem is that it finds the color in longer sequences: diff --git a/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/task.md b/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/task.md index 80ed625f..9a1923a7 100644 --- a/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/task.md +++ b/9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex/task.md @@ -5,11 +5,11 @@ Create a regexp to search HTML-colors written as `#ABCDEF`: first `#` and then 6 An example of use: ```js -let reg = /...your regexp.../ +let regexp = /...your regexp.../ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2 #12345678"; -alert( str.match(reg) ) // #121212,#AA00ef +alert( str.match(regexp) ) // #121212,#AA00ef ``` P.S. In this task we do not need other color formats like `#123` or `rgb(1,2,3)` etc. diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/solution.md b/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/solution.md index b3290607..0244963d 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/solution.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/solution.md @@ -5,11 +5,11 @@ An acceptable variant is `pattern:` -- the lazy quantifier makes the d Otherwise multiline comments won't be found: ```js run -let reg = //gs; +let regexp = //gs; let str = `... .. .. `; -alert( str.match(reg) ); // '', '' +alert( str.match(regexp) ); // '', '' ``` diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/task.md b/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/task.md index 81fd5c63..551d9c72 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/task.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/3-find-html-comments/task.md @@ -3,11 +3,11 @@ Find all HTML comments in the text: ```js -let reg = /your regexp/g; +let regexp = /your regexp/g; let str = `... .. .. `; -alert( str.match(reg) ); // '', '' +alert( str.match(regexp) ); // '', '' ``` diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/solution.md b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/solution.md index c453926f..b4d9f749 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/solution.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/solution.md @@ -2,9 +2,9 @@ The solution is `pattern:<[^<>]+>`. ```js run -let reg = /<[^<>]+>/g; +let regexp = /<[^<>]+>/g; let str = '<> '; -alert( str.match(reg) ); // '', '', '' +alert( str.match(regexp) ); // '', '', '' ``` diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md index e3c39c37..8e96c921 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md @@ -5,11 +5,11 @@ Create a regular expression to find all (opening and closing) HTML tags with the An example of use: ```js run -let reg = /your regexp/g; +let regexp = /your regexp/g; let str = '<> '; -alert( str.match(reg) ); // '', '', '' +alert( str.match(regexp) ); // '', '', '' ``` Here we assume that tag attributes may not contain `<` and `>` (inside squotes too), that simplifies things a bit. diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/article.md b/9-regular-expressions/10-regexp-greedy-and-lazy/article.md index e014c16d..4298e7c8 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/article.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/article.md @@ -17,11 +17,11 @@ A regular expression like `pattern:/".+"/g` (a quote, then something, then the o Let's try it: ```js run -let reg = /".+"/g; +let regexp = /".+"/g; let str = 'a "witch" and her "broom" is one'; -alert( str.match(reg) ); // "witch" and her "broom" +alert( str.match(regexp) ); // "witch" and her "broom" ``` ...We can see that it works not as intended! @@ -105,11 +105,11 @@ To make things clear: usually a question mark `pattern:?` is a quantifier by its The regexp `pattern:/".+?"/g` works as intended: it finds `match:"witch"` and `match:"broom"`: ```js run -let reg = /".+?"/g; +let regexp = /".+?"/g; let str = 'a "witch" and her "broom" is one'; -alert( str.match(reg) ); // witch, broom +alert( str.match(regexp) ); // witch, broom ``` To clearly understand the change, let's trace the search step by step. @@ -175,11 +175,11 @@ With regexps, there's often more than one way to do the same thing. In our case we can find quoted strings without lazy mode using the regexp `pattern:"[^"]+"`: ```js run -let reg = /"[^"]+"/g; +let regexp = /"[^"]+"/g; let str = 'a "witch" and her "broom" is one'; -alert( str.match(reg) ); // witch, broom +alert( str.match(regexp) ); // witch, broom ``` The regexp `pattern:"[^"]+"` gives correct results, because it looks for a quote `pattern:'"'` followed by one or more non-quotes `pattern:[^"]`, and then the closing quote. @@ -201,20 +201,20 @@ The first idea might be: `pattern://g`. Let's check it: ```js run let str = '......'; -let reg = //g; +let regexp = //g; // Works! -alert( str.match(reg) ); // +alert( str.match(regexp) ); // ``` It worked. But let's see what happens if there are many links in the text? ```js run let str = '...... ...'; -let reg = //g; +let regexp = //g; // Whoops! Two links in one match! -alert( str.match(reg) ); // ... +alert( str.match(regexp) ); // ... ``` Now the result is wrong for the same reason as our "witches" example. The quantifier `pattern:.*` took too many characters. @@ -230,10 +230,10 @@ Let's modify the pattern by making the quantifier `pattern:.*?` lazy: ```js run let str = '...... ...'; -let reg = //g; +let regexp = //g; // Works! -alert( str.match(reg) ); // , +alert( str.match(regexp) ); // , ``` Now it seems to work, there are two matches: @@ -247,10 +247,10 @@ Now it seems to work, there are two matches: ```js run let str = '......

...'; -let reg = //g; +let regexp = //g; // Wrong match! -alert( str.match(reg) ); // ...

+alert( str.match(regexp) ); // ...

``` Now it fails. The match includes not just a link, but also a lot of text after it, including ``. @@ -281,11 +281,11 @@ A working example: ```js run let str1 = '......

...'; let str2 = '...... ...'; -let reg = //g; +let regexp = //g; // Works! -alert( str1.match(reg) ); // null, no matches, that's correct -alert( str2.match(reg) ); // , +alert( str1.match(regexp) ); // null, no matches, that's correct +alert( str2.match(regexp) ); // , ``` ## Summary diff --git a/9-regular-expressions/11-regexp-groups/01-test-mac/solution.md b/9-regular-expressions/11-regexp-groups/01-test-mac/solution.md index c16f0565..26f7888f 100644 --- a/9-regular-expressions/11-regexp-groups/01-test-mac/solution.md +++ b/9-regular-expressions/11-regexp-groups/01-test-mac/solution.md @@ -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) ``` diff --git a/9-regular-expressions/11-regexp-groups/01-test-mac/task.md b/9-regular-expressions/11-regexp-groups/01-test-mac/task.md index e7265598..029a4803 100644 --- a/9-regular-expressions/11-regexp-groups/01-test-mac/task.md +++ b/9-regular-expressions/11-regexp-groups/01-test-mac/task.md @@ -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) ``` diff --git a/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md b/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md index e173aba6..0806dc4f 100644 --- a/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md +++ b/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md @@ -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 ``` diff --git a/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/task.md b/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/task.md index d87914e9..09108484 100644 --- a/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/task.md +++ b/9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/task.md @@ -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. diff --git a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md index 3155f13c..c4349f9a 100644 --- a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md +++ b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md @@ -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 ``` diff --git a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md index 45935068..4f5a73ff 100644 --- a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md +++ b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md @@ -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 ``` diff --git a/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md b/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md index decb074d..130c57be 100644 --- a/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md +++ b/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md @@ -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(); diff --git a/9-regular-expressions/11-regexp-groups/article.md b/9-regular-expressions/11-regexp-groups/article.md index 8a4fdd71..acc59b7c 100644 --- a/9-regular-expressions/11-regexp-groups/article.md +++ b/9-regular-expressions/11-regexp-groups/article.md @@ -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 = ''; -let reg = /<(([a-z]+)\s*([^>]*))>/; +let regexp = /<(([a-z]+)\s*([^>]*))>/; -let result = str.match(reg); +let result = str.match(regexp); alert(result[0]); // 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 diff --git a/9-regular-expressions/12-regexp-backreferences/article.md b/9-regular-expressions/12-regexp-backreferences/article.md index 07d2ca07..83beb803 100644 --- a/9-regular-expressions/12-regexp-backreferences/article.md +++ b/9-regular-expressions/12-regexp-backreferences/article.md @@ -17,10 +17,10 @@ We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, ```js run let str = `He said: "She's the one!".`; -let reg = /['"](.*?)['"]/g; +let regexp = /['"](.*?)['"]/g; // The result is not what we'd like to have -alert( str.match(reg) ); // "She' +alert( str.match(regexp) ); // "She' ``` As we can see, the pattern found an opening quote `match:"`, then the text is consumed till the other quote `match:'`, that closes the match. @@ -33,10 +33,10 @@ Here's the correct code: let str = `He said: "She's the one!".`; *!* -let reg = /(['"])(.*?)\1/g; +let regexp = /(['"])(.*?)\1/g; */!* -alert( str.match(reg) ); // "She's the one!" +alert( str.match(regexp) ); // "She's the one!" ``` Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group. @@ -65,8 +65,8 @@ In the example below the group with quotes is named `pattern:?`, so the b let str = `He said: "She's the one!".`; *!* -let reg = /(?['"])(.*?)\k/g; +let regexp = /(?['"])(.*?)\k/g; */!* -alert( str.match(reg) ); // "She's the one!" +alert( str.match(regexp) ); // "She's the one!" ``` diff --git a/9-regular-expressions/13-regexp-alternation/01-find-programming-language/solution.md b/9-regular-expressions/13-regexp-alternation/01-find-programming-language/solution.md index 3419aa49..e33f9cf2 100644 --- a/9-regular-expressions/13-regexp-alternation/01-find-programming-language/solution.md +++ b/9-regular-expressions/13-regexp-alternation/01-find-programming-language/solution.md @@ -4,11 +4,11 @@ The first idea can be to list the languages with `|` in-between. But that doesn't work right: ```js run -let reg = /Java|JavaScript|PHP|C|C\+\+/g; +let regexp = /Java|JavaScript|PHP|C|C\+\+/g; let str = "Java, JavaScript, PHP, C, C++"; -alert( str.match(reg) ); // Java,Java,PHP,C,C +alert( str.match(regexp) ); // Java,Java,PHP,C,C ``` The regular expression engine looks for alternations one-by-one. That is: first it checks if we have `match:Java`, otherwise -- looks for `match:JavaScript` and so on. @@ -25,9 +25,9 @@ There are two solutions for that problem: In action: ```js run -let reg = /Java(Script)?|C(\+\+)?|PHP/g; +let regexp = /Java(Script)?|C(\+\+)?|PHP/g; let str = "Java, JavaScript, PHP, C, C++"; -alert( str.match(reg) ); // Java,JavaScript,PHP,C,C++ +alert( str.match(regexp) ); // Java,JavaScript,PHP,C,C++ ``` diff --git a/9-regular-expressions/13-regexp-alternation/01-find-programming-language/task.md b/9-regular-expressions/13-regexp-alternation/01-find-programming-language/task.md index 61b9526f..e0f7af95 100644 --- a/9-regular-expressions/13-regexp-alternation/01-find-programming-language/task.md +++ b/9-regular-expressions/13-regexp-alternation/01-find-programming-language/task.md @@ -5,7 +5,7 @@ There are many programming languages, for instance Java, JavaScript, PHP, C, C++ Create a regexp that finds them in the string `subject:Java JavaScript PHP C++ C`: ```js -let reg = /your regexp/g; +let regexp = /your regexp/g; -alert("Java JavaScript PHP C++ C".match(reg)); // Java JavaScript PHP C++ C +alert("Java JavaScript PHP C++ C".match(regexp)); // Java JavaScript PHP C++ C ``` diff --git a/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md b/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md index dddaf962..971e2e62 100644 --- a/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md +++ b/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md @@ -8,7 +8,7 @@ The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`. In action: ```js run -let reg = /\[(b|url|quote)\].*?\[\/\1\]/gs; +let regexp = /\[(b|url|quote)\].*?\[\/\1\]/gs; let str = ` [b]hello![/b] @@ -17,7 +17,7 @@ let str = ` [/quote] `; -alert( str.match(reg) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote] +alert( str.match(regexp) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote] ``` Please note that we had to escape a slash for the closing tag `pattern:[/\1]`, because normally the slash closes the pattern. diff --git a/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/task.md b/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/task.md index 8cc59deb..2893a450 100644 --- a/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/task.md +++ b/9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/task.md @@ -32,17 +32,17 @@ Create a regexp to find all BB-tags with their contents. For instance: ```js -let reg = /your regexp/flags; +let regexp = /your regexp/flags; let str = "..[url]http://google.com[/url].."; -alert( str.match(reg) ); // [url]http://google.com[/url] +alert( str.match(regexp) ); // [url]http://google.com[/url] ``` If tags are nested, then we need the outer tag (if we want we can continue the search in its content): ```js -let reg = /your regexp/flags; +let regexp = /your regexp/flags; let str = "..[url][b]http://google.com[/b][/url].."; -alert( str.match(reg) ); // [url][b]http://google.com[/b][/url] +alert( str.match(regexp) ); // [url][b]http://google.com[/b][/url] ``` diff --git a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md index 143be870..5a007aee 100644 --- a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md +++ b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md @@ -10,8 +10,8 @@ Step by step: In action: ```js run -let reg = /"(\\.|[^"\\])*"/g; +let regexp = /"(\\.|[^"\\])*"/g; let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. '; -alert( str.match(reg) ); // "test me","Say \"Hello\"!","\\ \"" +alert( str.match(regexp) ); // "test me","Say \"Hello\"!","\\ \"" ``` diff --git a/9-regular-expressions/13-regexp-alternation/04-match-exact-tag/solution.md b/9-regular-expressions/13-regexp-alternation/04-match-exact-tag/solution.md index 70c4de91..5d4ba8d9 100644 --- a/9-regular-expressions/13-regexp-alternation/04-match-exact-tag/solution.md +++ b/9-regular-expressions/13-regexp-alternation/04-match-exact-tag/solution.md @@ -10,7 +10,7 @@ In the regexp language: `pattern:|\s.*?>)`. In action: ```js run -let reg = /|\s.*?>)/g; +let regexp = /|\s.*?>)/g; -alert( '