断言 / Assertions

Assertions indicate in some way that a match is possible. Assertions include look-ahead, look-behind, and conditional expressions.

断言assertions表明以某种方式会存在匹配,包括先行断言look-ahead后行断言look-behind、以及条件表达式conditional expressions

Types

类型

The ? character may also be used as a quantifier.

?也可以当作量词.

Characters Meaning
x(?=y) Lookahead assertion: Matches *x* only if *x* is followed by *y*. For example, /Jack(?=Sprat)/ matches “Jack” only if it is followed by “Sprat”. `/Jack(?=Sprat
x(?!y) Negative lookahead assertion: Matches *x* only if *x* is not followed by *y*. For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point. /\d+(?!\.)/.exec('3.141') matches “141” but not “3.
(?<=y)x Lookbehind assertion: Matches *x* only if *x* is preceded by *y*. For example, /(?<=Jack)Sprat/ matches “Sprat” only if it is preceded by “Jack”. `/(?<=Jack
(?<!y)x Negative lookbehind assertion: Matches *x* only if *x* is not preceded by *y*. For example, /(?<!-)\d+/ matches a number only if it is not preceded by a minus sign. /(?<!-)\d+/.exec('3') matches “3”. /(?<!-)\d+/.exec('-3') match is not found because the number is preceded by the minus sign.
字符 含义
x(?=y) 先行断言: y紧跟x的情况下匹配x。例如,对于/Jack(?=Sprat)/,“Jack”在跟有“Sprat”的情况下才会得到匹配./Jack(?=Sprat)/ “Jack”后跟有“Sprat”或“Frost”的情况下才会得到匹配。不过, 匹配结果不包括“Sprat”或“Frost”。
x(?!y) 负向先行断言: x后无y紧随的情况下匹配x。例如,对于/\d+(?!\。)/,数字后没有跟随小数点的情况下才会得到匹配。对于/\d+(?!\.)/.exec(3.141),“3”得到匹配,“141”则无。
(?<=y)x 后行断言: x紧随y的情况下匹配x。例如,对于/(?<=Jack)Sprat/,“Sprat”紧随“Jack”时才会得到匹配。对于/(?<=Jack)Sprat,“Sprat”在紧随“Jack”或“Tom”的情况下才会得到匹配。不过,匹配结果中不包括“Jack”或“Tom”。
(?<!y)x 负向后行断言: x不紧随y的情况下匹配x。例如,对于/(?<!-)\d+/,数字紧随-符号的情况下才会得到匹配。对于/(?<!-)\d+/.exec(3) ,“3”得到匹配。 而/(?<!-)\d+/.exec(-3)的结果无匹配,这是由于数字之前有-符号。

Examples

示例

Lookahead assertion

先行断言

1
2
3
4
5
6
7
8
// JS Lookahead assertion x(?=y)

let regex = /First(?= test)/g;

console.log('First test'.match(regex)); // [ 'First' ]
console.log('First peach'.match(regex)); // null
console.log('This is a First test in a year.'.match(regex)); // [ 'First' ]
console.log('This is a First peach in a month.'.match(regex)); // null

Basic negative lookahead assertion

负向先行断言

For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point. /\d+(?!\.)/.exec('3.141') matches “141” but not “3.

1
console.log(/\d+(?!\.)/g.exec('3.141')); // [ '141', index: 2, input: '3.141' ]

Different meaning of ‘?!’ combination usage in Assertions and Ranges

断言与范围组合使用情况下其中“?!”的不同含义

Different meaning of ?! combination usage in Assertions /x(?!y)/and Ranges [^?!].

1
2
3
4
5
6
7
8
let orangeNotLemon = "Do you want to have an orange? Yes, I do not want to have a lemon!";

// Different meaning of '?!' combination usage in Assertions /x(?!y)/ and Ranges /[^?!]/
let selectNotLemonRegex = /[^?!]+have(?! a lemon)[^?!]+[?!]/gi
console.log(orangeNotLemon.match(selectNotLemonRegex)); // [ 'Do you want to have an orange?' ]

let selectNotOrangeRegex = /[^?!]+have(?! an orange)[^?!]+[?!]/gi
console.log(orangeNotLemon.match(selectNotOrangeRegex)); // [ ' Yes, I do not want to have a lemon!' ]

Lookbehind assertion

后行断言

1
2
3
4
let oranges = ['ripe orange A ', 'green orange B', 'ripe orange C',];

let ripe_oranges = oranges.filter( fruit => fruit.match(/(?<=ripe )orange/));
console.log(ripe_oranges); // [ 'ripe orange A ', 'ripe orange C' ]
0%