RegExp.uts 11.3 KB
Newer Older
Y
yurj26 已提交
1 2 3 4
import { describe, test, expect, Result } from './tests.uts'

export function testRegExp(): Result {
    return describe("RegExp", () => {
杜庆泉's avatar
杜庆泉 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
      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
      })
      
Y
yurj26 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        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("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);
        })
杜庆泉's avatar
杜庆泉 已提交
93 94 95 96 97 98 99
		// 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);
        // })
Y
yurj26 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        
        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);
            
lizhongyi_'s avatar
lizhongyi_ 已提交
169 170
            // const re11 = /(?=a)?b/;
            // expect(re11.test("b")).toEqual(true);
Y
yurj26 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            
            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);
            
杜庆泉's avatar
杜庆泉 已提交
239
			
Y
yurj26 已提交
240 241 242 243
            const pattern8 = /https:\/\/(?!\w+\.(com|net))\w+\.\w+/;
            const result8 = pattern8.exec('https://example.org')!;
            expect(result8[0]).toEqual('https://example.org');
            
244 245
            // #ifndef WEB
            // 目前的测试环境safari版本为15,不支持?<
Y
yurj26 已提交
246 247 248 249 250 251 252
            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');
253
            // #endif
杜庆泉's avatar
杜庆泉 已提交
254 255 256 257 258
            
            
            const CHUNK_REGEXP =
                /^(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?\s*(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?$/
            const match2 = CHUNK_REGEXP.exec('none')
杜庆泉's avatar
杜庆泉 已提交
259 260
            expect(match2?.[0]).toEqual("none");
            expect(match2?.[1]).toEqual("none");
lizhongyi_'s avatar
lizhongyi_ 已提交
261 262 263
            // expect(JSON.stringify(match2![2])).toEqual("null");
            // expect(JSON.stringify(match2![3])).toEqual("null");
            // expect(JSON.stringify(match2![4])).toEqual("null");
杜庆泉's avatar
杜庆泉 已提交
264
            
Y
yurj26 已提交
265 266 267
        })
    })
}