reg->regexp
This commit is contained in:
parent
4232a53219
commit
32e20fc97c
35 changed files with 132 additions and 132 deletions
|
@ -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++
|
||||
```
|
||||
|
|
|
@ -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
|
||||
```
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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]
|
||||
```
|
||||
|
|
|
@ -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\"!","\\ \""
|
||||
```
|
||||
|
|
|
@ -10,7 +10,7 @@ In the regexp language: `pattern:<style(>|\s.*?>)`.
|
|||
In action:
|
||||
|
||||
```js run
|
||||
let reg = /<style(>|\s.*?>)/g;
|
||||
let regexp = /<style(>|\s.*?>)/g;
|
||||
|
||||
alert( '<style> <styler> <style test="...">'.match(reg) ); // <style>, <style test="...">
|
||||
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
|
||||
```
|
||||
|
|
|
@ -7,7 +7,7 @@ Write a regexp to find the tag `<style...>`. It should match the full tag: it ma
|
|||
For instance:
|
||||
|
||||
```js
|
||||
let reg = /your regexp/g;
|
||||
let regexp = /your regexp/g;
|
||||
|
||||
alert( '<style> <styler> <style test="...">'.match(reg) ); // <style>, <style test="...">
|
||||
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
|
||||
```
|
||||
|
|
|
@ -11,11 +11,11 @@ The corresponding regexp: `pattern:html|php|java(script)?`.
|
|||
A usage example:
|
||||
|
||||
```js run
|
||||
let reg = /html|php|css|java(script)?/gi;
|
||||
let regexp = /html|php|css|java(script)?/gi;
|
||||
|
||||
let str = "First HTML appeared, then CSS, then JavaScript";
|
||||
|
||||
alert( str.match(reg) ); // 'HTML', 'CSS', 'JavaScript'
|
||||
alert( str.match(regexp) ); // 'HTML', 'CSS', 'JavaScript'
|
||||
```
|
||||
|
||||
We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
|
||||
|
@ -64,7 +64,7 @@ But that's wrong, the alternation should only be used in the "hours" part of the
|
|||
The final solution:
|
||||
|
||||
```js run
|
||||
let reg = /([01]\d|2[0-3]):[0-5]\d/g;
|
||||
let regexp = /([01]\d|2[0-3]):[0-5]\d/g;
|
||||
|
||||
alert("00:00 10:10 23:59 25:99 1:2".match(reg)); // 00:00,10:10,23:59
|
||||
alert("00:00 10:10 23:59 25:99 1:2".match(regexp)); // 00:00,10:10,23:59
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue