# RegExp RegExp 对象用于将文本与一个模式匹配。 ### Constructor(pattern) ### Constructor(pattern, flags?)_1 ## 实例属性 ### flags ```ts console.log(/foo/ig.flags); // expected output: "gi" console.log(/bar/myu.flags); // expected output: "muy" ``` ### source ```ts const regex1 = /fooBar/ig; console.log(regex1.source); // expected output: "fooBar" console.log(new RegExp().source); // expected output: "(?:)" ``` ### global ```ts var regex = new RegExp("foo", "g") console.log(regex.global) // true // expected output: "muy" ``` ### ignoreCase ### multiline ```ts var regex = new RegExp("foo", "m"); console.log(regex.multiline); // expected output: true ``` ### lastIndex ```ts const regex1 = new RegExp('foo', 'g'); const str1 = 'table football, foosball'; regex1.test(str1); console.log(regex1.lastIndex); // expected output: 9 regex1.test(str1); console.log(regex1.lastIndex); // expected output: 19 ``` ## 实例方法 ### exec(string) ```ts const regex1 = RegExp('foo*', 'g'); const str1 = 'table football, foosball'; let array1; while ((array1 = regex1.exec(str1)) !== null) { console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`); // expected output: "Found foo. Next starts at 9." // expected output: "Found foo. Next starts at 19." } ``` ### test(string) ```ts const str = 'table football'; const regex = new RegExp('foo*'); const globalRegex = new RegExp('foo*', 'g'); console.log(regex.test(str)); // expected output: true console.log(globalRegex.lastIndex); // expected output: 0 console.log(globalRegex.test(str)); // expected output: true console.log(globalRegex.lastIndex); // expected output: 9 console.log(globalRegex.test(str)); // expected output: false ``` ## Bug & Tips@tips * 目前 RegExp 类型编译到 kotlin 为 io.dcloud.uts.UTSRegExp