Another RayJune

Regex from MDN

Collect some Regex concepts and demos from MDN.
(Some of these demos modified by myself).

RegExp As Object

1
2
3
4
var re = new RegExp("Shelley\s+Powers");
console.log(typeof re); // object
console.log(Object.prototype.toString.call(re)); // [object RegExp]

RegExp methods

test: quickly and simple

Returns true or false

1
2
3
4
5
6
7
8
var demo = 'abc rayjune';
var reg = /rayjune/;
console.log(reg.test(demo)); // true
var str = 'hello world!';
var result = /^hello/.test(str);
console.log(result); // true

If the regex has the global flag set, test() will advance the lastIndex of the regex.

1
2
3
4
5
6
7
var regex = /foo/g;
// regex.lastIndex is at 0
regex.test('foo'); // true
// regex.lastIndex is now at 3
regex.test('foo'); // false

exec: overall but complicated

The exec() method executes a search for a match in a specified string. Returns a result array, or null.

1
2
3
4
5
var str = 'hello -rayjune';
var re = /hello/;
console.log(re.exec(re)); // ["hello", index: 0, input: "hello -rayjune"]
// the same as match

once use ‘\g’

1
2
3
4
5
var reg = /hello/g;
var doubleStr = 'hello -rayjune hello -liga';
console.log(reg.exec(doubleStr)); // ["hello", index: 0, input: "hello -rayjune hello -liga"]
console.log(reg.exec(doubleStr)); // ["hello", index: 15, input: "hello -rayjune hello -liga"]
console.log(reg.exec(doubleStr)); // null

and the re has lastIndex property

1
2
3
4
5
6
7
8
var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}

String method

search: just index

The index of the first match between the regular expression and the given string;
if not found, -1.

1
2
3
4
5
var str = "hey JudE";
var re = /[A-Z]/g;
var re2 = /[.]/g;
console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation

match: return eligible array

An Array containing the entire match result and any parentheses-captured matched results;
null if there were no matches.

1
2
3
4
var str = 'hello -rayjune';
var re = /hello/;
console.log(str.match(re)); // ["hello", index: 0, input: "hello -rayjune"]

once use ‘\g’

1
2
3
var reg = /hello/g;
var doubleStr = 'hello -rayjune hello -liga';
console.log(doubleStr.match(re)); // (2) ["hello", "hello"]

replace: (regexp|substr, newSubStr|function)

1
2
3
4
var a = 'abc de';
var re = /d/;
console.log(a.replace(re, 'f')); // abc fe

Switching words in a string

1
2
3
4
var re = /(\w+)\s(\w+)/;
var str = 'RayJune Xue';
var newstr = str.replace(re, '$2, $1');
console.log(newstr); // Xue, RayJune

split: simple

1
2
3
4
var re = /like/;
var str = 'rayjune like reading like coding like bloging';
console.log(str.split(re)); // (4) ["rayjune ", " reading ", " coding ", " bloging"]

Special characters

\s

1
2
3
4
5
var re = /Shelley\s+Powers/;
// Shelley Powers
// Shelley Powers
// Shelley Powers

. (The decimal point)

matches any single character except the newline character.

1
2
var re = /.n/;
'na rayjune'.match(re); // ["un", index: 7, input: "na rayjune"]

not match na

(x)

Matches ‘x’ and remembers the match, as the following example shows. The parentheses are called capturing parentheses.

1
2
3
4
5
6
var re = /(a)(b)/;
'ab'.match(re); // (3) ["ab", "a", "b", index: 0, input: "ab"]
re.exec('ab') // (3) ["ab", "a", "b", index: 0, input: "ab"]
'ab'.search(re); // 0
'ab'.replace(re, '$2, $1'); // b, a

(?:x)

Matches ‘x’ but does not remember the match.

1
2
var re = /(?:a)(?:b)/;
'ab'.match(re); ["ab", index: 0, input: "ab"]

And let you define subexpressions for regular expression operators to work with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var re1 = /foo{1,2}/;
console.log('foofoo'.match(re1)); // ["foo", index: 0, input: "foofoo"]
console.log('foooo'.match(re1)); // ["fooo", index: 0, input: "foooo"]
var re2 = /(?:foo){1,2}/;
console.log('foofoo'.match(re2)); // ["foofoo", index: 0, input: "foofoo"]
console.log('foooo'.match(re2)); // ["foo", index: 0, input: "foooo"]
```
If the expression was /foo{1,2}/, the {1,2} characters would apply `only to the last 'o'` in 'foo'. With the non-capturing parentheses, the {1,2} applies to the `entire word 'foo'`.
### x(?=y)
Matches 'x' `only if 'x' is followed by 'y'`
```javascript
var re = /ray(?=june)/;
console.log(re.test('ray')); // false
console.log(re.test('rayjune')); // true

x(?!y)

Matches ‘x’ only if ‘x’ is not followed by ‘y’

1
2
3
var re = /ray(?!june)/;
console.log(re.test('ray')); // true
console.log(re.test('rayjune')); // false

[xyz]

This pattern type matches any one of the characters in the brackets, including escape sequences.

1
console.log(/[\/]/.test('/')); // true

[^xyz]

It matches anything that is not enclosed in the brackets

1
console.log('abcdef'.match(/[^a-c]/g)); // (3) ["d", "e", "f"]

\b

Matches a word boundary.

1
console.log('moon'.match(/\bm/)); // ["m", index: 0, input: "moon"]

\B

Matches a non-word boundary.

1
console.log('possibly yesterday.'.match(/y\B./)); // ["ye", index: 9, input: "possibly yesterday."]

\d

Matches a digit character. Equivalent to [0-9].

\D

Matches a non-digit character. Equivalent to [^0-9].

\r

Matches a carriage return

\s

Matches a single white space character, including space, tab, form feed, line feed.

\w

Matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_]

1
console.log('ray_june'.match(/\w/g)); (8) ["r", "a", "y", "_", "j", "u", "n", "e"]

\W

Matches any non-word character. Equivalent to [^A-Za-z0-9_].

文章标题:Regex from MDN

文章作者:RayJune

时间地点:下午9:28,于又玄图书馆

原始链接:http://rayjune.xyz/2017/10/10/Regex-from-MDN/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。