这个RE接受的一般字符串的单词描述是什么?
// _hello.js to testing
exports.hello = function(string) {
return 'hello ' + string;
}
// test file
var assert = require('assert');
var hello = require('./_hello.js');
describe('hello function', function() {
it('test output', function () {
assert.equal('hello world', hello.hello('world'));
});
});
我首先认为它只是分别以01和11开头和结尾的任何字符串,其间有任意数量的交替1和0,但整个正则表达式包含在(01(10)*11)*
中的事实使这种描述无效。
答案 0 :(得分:2)
(01(10)*11)*
由以下组成的组的零次或多次重复:
此模式可以匹配,例如:
0111 => 01##11
011011 => 01#10#11
01101011 => 01#1010#11
0110101011 => 01#101010#11
011011011011 => 01#10#11|01#10#11
011011011011011011 => 01#10#11|01#10#11|01#10#11
011101101101101011 => 01##11|01#10#11|01#1010#11
011010110110110111 => 01#1010#11|01#10#11|01##11
答案 1 :(得分:1)
( # opens capture group 1
01 # matches 01, literally
( # opens capture group 2
10 # matches 10, literally
) # closes capture group 2
* # Repeats previous group zero or more times
11 # matches 11, literally
)
* # Repeats previous group zero or more times
只有在满足捕获组1的所有其他组件时才会捕获组2。如果在任意数量10
之后找不到11,则回溯并继续查看。
这将匹配01
后跟任意数量的10
,包括0 / none,然后是`11。
这将匹配
- nothing, zero repeats of the outer group
- regexes that can match an empty string are useless for
- validation, especially without assertions.
0111 - zero occurrencess of 10
011011 - one occurrence of 10.
01101011 - two occurrences of 10
011101101011 - one occurrence of CG1 where CG2 had no iterations,
- and one where it had 2
- this can and will continue to an infinite number of matches
请阅读Quantifiers,老实说,您可以在regex101上进行大量的学习实验。 Here's your regex setup there.所有紫色点都是因为你的表达式可以匹配空字符串。