提交 4f7af32c 编写于 作者: M mahaifeng

[regexp]去除文档中手动生成的代码,添加注释

上级 5d05f53b
import { describe, test, expect, Result } from './tests.uts'
export function testRegExp(): Result {
return describe("RegExp", () => {
test("constructor", () => {
// #ifdef APP-ANDROID
const str = 'table football';
const regex = new RegExp('foo*');
const globalRegex = new RegExp();
expect(regex.test(str)).toEqual(true);
expect(globalRegex.test(str)).toEqual(true);
let r2 = new RegExp(/ab+c/);
expect(r2.test(str)).toEqual(false);
let r3 = new RegExp(/foo/);
expect(r3.test(str)).toEqual(true);
// #endif
})
test("dotAll", () => {
const regex1 = new RegExp('foo', 's');
expect(regex1.dotAll).toEqual(true);
const regex2 = new RegExp('bar');
expect(regex2.dotAll).toEqual(false);
})
test("flags", () => {
const regex1 = new RegExp('foo', 'ig');
expect(regex1.flags).toEqual("gi");
const regex2 = new RegExp('bar', 'myu');
expect(regex2.flags).toEqual("muy");
const regex3 = new RegExp('bar');
expect(regex3.flags).toEqual("");
})
test("global", () => {
const regex1 = new RegExp('foo', 'g');
expect(regex1.global).toEqual(true);
const regex2 = new RegExp('bar');
expect(regex2.global).toEqual(false);
})
test("hasIndices", () => {
const regex1 = new RegExp('foo', 'd');
expect(regex1.hasIndices).toEqual(true);
const regex2 = new RegExp('bar');
expect(regex2.hasIndices).toEqual(false);
})
test("ignoreCase", () => {
const regex1 = new RegExp('foo', 'i');
expect(regex1.ignoreCase).toEqual(true);
const regex2 = new RegExp('bar');
expect(regex2.ignoreCase).toEqual(false);
})
test("RegExpExecArray", () => {
// #ifdef APP-ANDROID
const regex1 = RegExp('foo*', 'g');
const str1 = 'table football, foosball';
let array1:RegExpExecArray = regex1.exec(str1)!;
console.log(array1[0] == 'foo')
expect(array1[0] == 'foo').toEqual(true);
// #endif
})
test("lastIndex", () => {
const regex = /ab/g;
const str = 'ab ab ab';
const result1 = regex.exec(str)!;
expect(result1.index).toEqual(0);
expect(regex.lastIndex).toEqual(2);
const result2 = regex.exec(str)!;
expect(result2.index).toEqual(3);
expect(regex.lastIndex).toEqual(5);
const result3 = regex.exec(str)!;
expect(result3.index).toEqual(6);
expect(regex.lastIndex).toEqual(8);
})
test("multiline", () => {
const regex1 = new RegExp('foo', 'm');
expect(regex1.multiline).toEqual(true);
const regex2 = new RegExp('bar');
expect(regex2.multiline).toEqual(false);
})
test("source", () => {
const regex1 = new RegExp('foo', 'ig');
expect(regex1.source).toEqual("foo");
const regex2 = new RegExp('bar');
expect(regex2.source).toEqual("bar");
const regex3 = /\w+/gi;
expect(regex3.source).toEqual("\\w+");
const regex4 = new RegExp('\\d+', 'ig');;
expect(regex4.source).toEqual("\\d+");
})
test("sticky", () => {
const str1 = 'table football';
const regex1 = new RegExp('foo', 'y');
regex1.lastIndex = 6;
expect(regex1.sticky).toEqual(true);
expect(regex1.test(str1)).toEqual(true);
expect(regex1.test(str1)).toEqual(false);
})
// android 暂时不支持 unicode
// test("unicode", () => {
// const regex1 = new RegExp('foo', 'u');
// expect(regex1.unicode).toEqual(true);
// const regex2 = new RegExp('bar');
// expect(regex2.unicode).toEqual(false);
// })
test('test', () => {
const str = 'table football';
const regex = new RegExp('foo*');
const globalRegex = new RegExp('foo*', 'g');
expect(regex.test(str)).toEqual(true);
expect(globalRegex.lastIndex).toEqual(0);
expect(globalRegex.test(str)).toEqual(true);
expect(globalRegex.lastIndex).toEqual(9);
expect(globalRegex.test(str)).toEqual(false);
const re = new RegExp('hello', 'g');
const str1 = 'hello world';
const str2 = 'hello there';
expect(re.test(str1)).toEqual(true);
expect(re.lastIndex).toEqual(5);
expect(re.test(str2)).toEqual(false);
expect(re.lastIndex).toEqual(0);
const re1 = new RegExp('(\\d{3})-(\\d{2})-(\\d{4})');
expect(re1.test('123-45-6789')).toEqual(true);
expect(re1.test('123-45-678')).toEqual(false);
const re2 = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?(\/[\w\-./?%&=]*)?$/;
expect(re2.test('https://www.example.com')).toEqual(true);
expect(re2.test('http://127.0.0.1:8080')).toEqual(true);
expect(re2.test('www.example.com')).toEqual(false);
expect(re2.test('https:/www.example.com')).toEqual(false);
expect(re2.test('https:/www.example.com@foo')).toEqual(false);
expect(re2.test('123')).toEqual(false);
const re3 = /^1[3-9]\d{9}$/;
expect(re3.test('13912345678')).toEqual(true);
expect(re3.test('15812345678')).toEqual(true);
expect(re3.test('12912345678')).toEqual(false);
expect(re3.test('1391234')).toEqual(false);
expect(re3.test('1581234567a')).toEqual(false);
const re4 = /[\u4E00-\u9FA5]/;
expect(re4.test('测试')).toEqual(true);
expect(re4.test('123')).toEqual(false);
expect(re4.test('abc')).toEqual(false);
const re5 = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
expect(re5.test('#b8b8b8')).toEqual(true);
expect(re5.test('#b8b')).toEqual(true);
expect(re5.test('#b8b8b81')).toEqual(false);
expect(re5.test('#b8b8b')).toEqual(false);
expect(re5.test('b8b8b')).toEqual(false);
const re6 = new RegExp('hello', 'i');
expect(re6.test('Hello')).toEqual(true);
const re7 = new RegExp('^Hello', 'm');
expect(re7.test('Hello\nWorld')).toEqual(true);
// const re8 = new RegExp('\\u{1F602}', 'u');
// expect(re8.test('😂')).toEqual(true);
// expect(re8.test('😊')).toEqual(false);
const re9 = new RegExp('a\\d', 'y');
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(false);
const re10 = new RegExp('^foo.bar$', 's');
expect(re10.test('foo\nbar')).toEqual(true);
// const re11 = /(?=a)?b/;
// expect(re11.test("b")).toEqual(true);
const isValidIdentifierName = (str: string): boolean => {
const reg =
/^(?!(?:break|case|catch)$)[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
return reg.test(str);
}
expect(isValidIdentifierName("break")).toEqual(false);
expect(isValidIdentifierName("foo")).toEqual(true);
expect(isValidIdentifierName("cases")).toEqual(true);
const isASCIIIDPart = (char: string): boolean => {
return /^(?=\p{ASCII}$)\p{ID_Start}$/u.test(char);
}
expect(isASCIIIDPart("a")).toEqual(true);
expect(isASCIIIDPart("α")).toEqual(false);
expect(isASCIIIDPart(":")).toEqual(false);
})
test('exec', () => {
const pattern = new RegExp('hello');
const result = pattern.exec('hi there');
expect(result).toEqual(null);
const pattern1 = new RegExp('hello');
const result1 = pattern1.exec('hello world')!;
expect(result1[0]).toEqual('hello');
expect(result1.index).toEqual(0);
expect(result1.input).toEqual("hello world");
const pattern2 = /quick\s(?<color>brown).+?(jumps)/igd;
const result2 = pattern2.exec('The Quick Brown Fox Jumps Over The Lazy Dog')!;
expect(result2[0]).toEqual("Quick Brown Fox Jumps");
expect(result2[1]).toEqual("Brown");
expect(result2[2]).toEqual("Jumps");
expect(result2.index).toEqual(4);
expect(result2.input).toEqual("The Quick Brown Fox Jumps Over The Lazy Dog");
// expect(result2.groups).toEqual({color: "Brown"});
const pattern3 = /(\d{4})-(\d{2})-(\d{2})/g;
const result3 = pattern3.exec("2023-02-01")!;
expect(result3[0]).toEqual("2023-02-01");
expect(result3[1]).toEqual("2023");
expect(result3[2]).toEqual("02");
expect(result3[3]).toEqual("01");
expect(result3.index).toEqual(0);
expect(result3.input).toEqual("2023-02-01");
const pattern4 = /(?=(a+))a*b\1/;
const result4 = pattern4.exec("baabac")!;
expect(result4[0]).toEqual("aba");
expect(result4[1]).toEqual("a");
expect(result4.index).toEqual(2);
expect(result4.input).toEqual("baabac");
const pattern5 = /(.*?)a(?!(a+)b\1c)\1(.*)/;
const result5 = pattern5.exec("baaabaac")!;
expect(result5[0]).toEqual("baaabaac");
expect(result5[1]).toEqual("baa");
expect(result5[3]).toEqual("c");
expect(result5.index).toEqual(0);
const pattern6 = /^.*?(?=[,.])/;
const result6 = pattern6.exec("Hello, world!")!;
expect(result6[0]).toEqual("Hello");
const pattern7 = /\d{4}-(?=\d{2}-\d{2})\d{2}-\d{2}/;
const result7 = pattern7.exec('2023/05-26');
expect(result7).toEqual(null);
const pattern8 = /https:\/\/(?!\w+\.(com|net))\w+\.\w+/;
const result8 = pattern8.exec('https://example.org')!;
expect(result8[0]).toEqual('https://example.org');
// #ifndef WEB
// 目前的测试环境safari版本为15,不支持?<
const pattern9 = /(?<=@)\w+/;
const result9 = pattern9.exec('Email: john@example.com')!;
expect(result9[0]).toEqual('example');
const pattern10 = /(?<!@)\w+/;
const result10 = pattern10.exec('Username: john')!;
expect(result10[0]).toEqual('Username');
// #endif
const CHUNK_REGEXP =
/^(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?\s*(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?$/
const match2 = CHUNK_REGEXP.exec('none')
expect(match2![0]).toEqual("none");
expect(match2![1]).toEqual("none");
// expect(JSON.stringify(match2![2])).toEqual("null");
// expect(JSON.stringify(match2![3])).toEqual("null");
// expect(JSON.stringify(match2![4])).toEqual("null");
})
export function testRegExp() : Result {
return describe("RegExp", () => {
test("constructor", () => {
// #ifdef APP-ANDROID
const str = 'table football';
const regex = new RegExp('foo*');
const globalRegex = new RegExp();
expect(regex.test(str)).toEqual(true);
expect(globalRegex.test(str)).toEqual(true);
let r2 = new RegExp(/ab+c/);
expect(r2.test(str)).toEqual(false);
let r3 = new RegExp(/foo/);
expect(r3.test(str)).toEqual(true);
// #endif
})
test("dotAll", () => {
// #TEST RegExp.dotAll
const regex1 = new RegExp('foo', 's');
console.log(regex1.dotAll);
// expected output: true
const regex2 = new RegExp('bar');
console.log(regex2.dotAll);
// expected output: false
// #END
expect(regex1.dotAll).toEqual(true);
expect(regex2.dotAll).toEqual(false);
});
test("flags", () => {
// #TEST RegExp.flags
const regex1 = new RegExp('foo', 'ig');
console.log(regex1.flags);
// expected output: "gi"
const regex2 = new RegExp('bar', 'myu');
console.log(regex2.flags);
// expected output: "muy"
const regex3 = new RegExp('bar');
console.log(regex3.flags);
// expected output: ""
// #END
expect(regex1.flags).toEqual("gi");
expect(regex2.flags).toEqual("muy");
expect(regex3.flags).toEqual("");
});
test("global", () => {
// #TEST RegExp.global
const regex1 = new RegExp('foo', 'g');
console.log(regex1.global);
// expected output: true
const regex2 = new RegExp('bar');
console.log(regex2.global);
// expected output: false
// #END
expect(regex1.global).toEqual(true);
expect(regex2.global).toEqual(false);
});
test("hasIndices", () => {
// #TEST RegExp.hasIndices
const regex1 = new RegExp('foo', 'd');
console.log(regex1.hasIndices);
// expected output: true
const regex2 = new RegExp('bar');
console.log(regex2.hasIndices);
// expected output: false
// #END
expect(regex1.hasIndices).toEqual(true);
expect(regex2.hasIndices).toEqual(false);
});
test("ignoreCase", () => {
// #TEST RegExp.ignoreCase
const regex1 = new RegExp('foo', 'i');
console.log(regex1.ignoreCase);
// expected output: true
const regex2 = new RegExp('bar');
console.log(regex2.ignoreCase);
// expected output: false
// #END
expect(regex1.ignoreCase).toEqual(true);
expect(regex2.ignoreCase).toEqual(false);
});
test("RegExpExecArray", () => {
// #ifdef APP-ANDROID
const regex1 = RegExp('foo*', 'g');
const str1 = 'table football, foosball';
let array1 : RegExpExecArray = regex1.exec(str1)!;
console.log(array1[0] == 'foo')
expect(array1[0] == 'foo').toEqual(true);
// #endif
})
test("lastIndex", () => {
// #TEST RegExp.lastIndex
const regex = /ab/g;
const str = 'ab ab ab';
const result1 = regex.exec(str)!;
console.log(result1.index);
console.log(regex.lastIndex);
// expected output:
// result1.index: 0
// regex.lastIndex: 2
const result2 = regex.exec(str)!;
console.log(result2.index);
console.log(regex.lastIndex);
// expected output:
// result2.index: 3
// regex.lastIndex: 5
const result3 = regex.exec(str)!;
console.log(result3.index);
console.log(regex.lastIndex);
// expected output:
// result3.index: 6
// regex.lastIndex: 8
// #END
expect(result1.index).toEqual(0);
expect(regex.lastIndex).toEqual(2);
expect(result2.index).toEqual(3);
expect(regex.lastIndex).toEqual(5);
expect(result3.index).toEqual(6);
expect(regex.lastIndex).toEqual(8);
});
test("multiline", () => {
// #TEST RegExp.multiline
const regex1 = new RegExp('foo', 'm');
console.log(regex1.multiline);
// expected output: true
const regex2 = new RegExp('bar');
console.log(regex2.multiline);
// expected output: false
// #END
expect(regex1.multiline).toEqual(true);
expect(regex2.multiline).toEqual(false);
});
test("source", () => {
// #TEST RegExp.source
const regex1 = new RegExp('foo', 'ig');
console.log(regex1.source);
// expected output: "foo"
const regex2 = new RegExp('bar');
console.log(regex2.source);
// expected output: "bar"
const regex3 = /\w+/gi;
console.log(regex3.source);
// expected output: "\\w+"
const regex4 = new RegExp('\\d+', 'ig');
console.log(regex4.source);
// expected output: "\\d+"
// #END
expect(regex1.source).toEqual("foo");
expect(regex2.source).toEqual("bar");
expect(regex3.source).toEqual("\\w+");
expect(regex4.source).toEqual("\\d+");
});
test("sticky", () => {
// #TEST RegExp.sticky
const str1 = 'table football';
const regex1 = new RegExp('foo', 'y');
regex1.lastIndex = 6;
console.log(regex1.sticky);
// expected output: true
console.log(regex1.test(str1));
// expected output: true (the match starts at index 6)
// Reset the lastIndex to test non-match
regex1.lastIndex = 0;
console.log(regex1.test(str1));
// expected output: false (subsequent tests should fail since the lastIndex is at 0)
// #END
expect(regex1.sticky).toEqual(true);
expect(regex1.test(str1)).toEqual(true);
expect(regex1.test(str1)).toEqual(false);
});
// android 暂时不支持 unicode
// test("unicode", () => {
// const regex1 = new RegExp('foo', 'u');
// expect(regex1.unicode).toEqual(true);
// const regex2 = new RegExp('bar');
// expect(regex2.unicode).toEqual(false);
// })
test('test', () => {
// #TEST RegExp.test
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
// #END
expect(regex.test(str)).toEqual(true);
expect(globalRegex.lastIndex).toEqual(0);
expect(globalRegex.test(str)).toEqual(true);
expect(globalRegex.lastIndex).toEqual(9);
expect(globalRegex.test(str)).toEqual(false);
const re = new RegExp('hello', 'g');
const str1 = 'hello world';
const str2 = 'hello there';
expect(re.test(str1)).toEqual(true);
expect(re.lastIndex).toEqual(5);
expect(re.test(str2)).toEqual(false);
expect(re.lastIndex).toEqual(0);
const re1 = new RegExp('(\\d{3})-(\\d{2})-(\\d{4})');
expect(re1.test('123-45-6789')).toEqual(true);
expect(re1.test('123-45-678')).toEqual(false);
const re2 = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?(\/[\w\-./?%&=]*)?$/;
expect(re2.test('https://www.example.com')).toEqual(true);
expect(re2.test('http://127.0.0.1:8080')).toEqual(true);
expect(re2.test('www.example.com')).toEqual(false);
expect(re2.test('https:/www.example.com')).toEqual(false);
expect(re2.test('https:/www.example.com@foo')).toEqual(false);
expect(re2.test('123')).toEqual(false);
const re3 = /^1[3-9]\d{9}$/;
expect(re3.test('13912345678')).toEqual(true);
expect(re3.test('15812345678')).toEqual(true);
expect(re3.test('12912345678')).toEqual(false);
expect(re3.test('1391234')).toEqual(false);
expect(re3.test('1581234567a')).toEqual(false);
const re4 = /[\u4E00-\u9FA5]/;
expect(re4.test('测试')).toEqual(true);
expect(re4.test('123')).toEqual(false);
expect(re4.test('abc')).toEqual(false);
const re5 = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
expect(re5.test('#b8b8b8')).toEqual(true);
expect(re5.test('#b8b')).toEqual(true);
expect(re5.test('#b8b8b81')).toEqual(false);
expect(re5.test('#b8b8b')).toEqual(false);
expect(re5.test('b8b8b')).toEqual(false);
const re6 = new RegExp('hello', 'i');
expect(re6.test('Hello')).toEqual(true);
const re7 = new RegExp('^Hello', 'm');
expect(re7.test('Hello\nWorld')).toEqual(true);
// const re8 = new RegExp('\\u{1F602}', 'u');
// expect(re8.test('😂')).toEqual(true);
// expect(re8.test('😊')).toEqual(false);
const re9 = new RegExp('a\\d', 'y');
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(true);
expect(re9.test('a1a2a3')).toEqual(false);
const re10 = new RegExp('^foo.bar$', 's');
expect(re10.test('foo\nbar')).toEqual(true);
// const re11 = /(?=a)?b/;
// expect(re11.test("b")).toEqual(true);
const isValidIdentifierName = (str : string) : boolean => {
const reg =
/^(?!(?:break|case|catch)$)[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
return reg.test(str);
}
expect(isValidIdentifierName("break")).toEqual(false);
expect(isValidIdentifierName("foo")).toEqual(true);
expect(isValidIdentifierName("cases")).toEqual(true);
const isASCIIIDPart = (char : string) : boolean => {
return /^(?=\p{ASCII}$)\p{ID_Start}$/u.test(char);
}
expect(isASCIIIDPart("a")).toEqual(true);
expect(isASCIIIDPart("α")).toEqual(false);
expect(isASCIIIDPart(":")).toEqual(false);
})
test('exec', () => {
const pattern = new RegExp('hello');
const result = pattern.exec('hi there');
expect(result).toEqual(null);
// #TEST RegExp.exec
const pattern1 = new RegExp('hello');
const result1 = pattern1.exec('hello world')!;
console.log(result1[0]) //'hello'
console.log(result1.index) //0
// #END
expect(result1[0]).toEqual('hello');
expect(result1.index).toEqual(0);
expect(result1.input).toEqual("hello world");
const pattern2 = /quick\s(?<color>brown).+?(jumps)/igd;
const result2 = pattern2.exec('The Quick Brown Fox Jumps Over The Lazy Dog')!;
expect(result2[0]).toEqual("Quick Brown Fox Jumps");
expect(result2[1]).toEqual("Brown");
expect(result2[2]).toEqual("Jumps");
expect(result2.index).toEqual(4);
expect(result2.input).toEqual("The Quick Brown Fox Jumps Over The Lazy Dog");
// expect(result2.groups).toEqual({color: "Brown"});
const pattern3 = /(\d{4})-(\d{2})-(\d{2})/g;
const result3 = pattern3.exec("2023-02-01")!;
expect(result3[0]).toEqual("2023-02-01");
expect(result3[1]).toEqual("2023");
expect(result3[2]).toEqual("02");
expect(result3[3]).toEqual("01");
expect(result3.index).toEqual(0);
expect(result3.input).toEqual("2023-02-01");
const pattern4 = /(?=(a+))a*b\1/;
const result4 = pattern4.exec("baabac")!;
expect(result4[0]).toEqual("aba");
expect(result4[1]).toEqual("a");
expect(result4.index).toEqual(2);
expect(result4.input).toEqual("baabac");
const pattern5 = /(.*?)a(?!(a+)b\1c)\1(.*)/;
const result5 = pattern5.exec("baaabaac")!;
expect(result5[0]).toEqual("baaabaac");
expect(result5[1]).toEqual("baa");
expect(result5[3]).toEqual("c");
expect(result5.index).toEqual(0);
const pattern6 = /^.*?(?=[,.])/;
const result6 = pattern6.exec("Hello, world!")!;
expect(result6[0]).toEqual("Hello");
const pattern7 = /\d{4}-(?=\d{2}-\d{2})\d{2}-\d{2}/;
const result7 = pattern7.exec('2023/05-26');
expect(result7).toEqual(null);
const pattern8 = /https:\/\/(?!\w+\.(com|net))\w+\.\w+/;
const result8 = pattern8.exec('https://example.org')!;
expect(result8[0]).toEqual('https://example.org');
// #ifndef WEB
// 目前的测试环境safari版本为15,不支持?<
const pattern9 = /(?<=@)\w+/;
const result9 = pattern9.exec('Email: john@example.com')!;
expect(result9[0]).toEqual('example');
const pattern10 = /(?<!@)\w+/;
const result10 = pattern10.exec('Username: john')!;
expect(result10[0]).toEqual('Username');
// #endif
const CHUNK_REGEXP =
/^(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?\s*(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?$/
const match2 = CHUNK_REGEXP.exec('none')
expect(match2![0]).toEqual("none");
expect(match2![1]).toEqual("none");
// expect(JSON.stringify(match2![2])).toEqual("null");
// expect(JSON.stringify(match2![3])).toEqual("null");
// expect(JSON.stringify(match2![4])).toEqual("null");
})
}
})
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册