1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:regexp 18 * @tc.desc:test Regexp 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22{ 23 let str = ""; 24 let regexp = /[]/; 25 print(JSON.stringify(str.replace(regexp,"b"))); 26} 27{ 28 let str = ""; 29 let regexp = /[]/g; 30 print(JSON.stringify(str.replace(regexp,"b"))); 31} 32{ 33 let str = ""; 34 let regexp = /[]/u; 35 print(JSON.stringify(str.replace(regexp,"b"))); 36} 37{ 38 let str = ""; 39 let regexp = /[\]/; 40 print(JSON.stringify(str.replace(regexp,"b"))); 41} 42 43 44var reg = /[\x5d-\x7e]/i; 45var result = reg.test("a"); 46print(result); 47 48var reg1 = new RegExp("^[-+]?([0-9]+)?(\\٫[0-9]{1,})?$"); 49var result1 = reg1.test('0٫0000000000001'); 50print(result1); 51 52var reg2 = /^[Α-ώ]+$/i 53print(reg2.test('άέήίΰϊϋόύώ')); 54 55print(reg2.test('ΆΈΉΊΪΫΎΏ')); 56 57print(reg2.test('αβγδεζηθικλμνξοπρςστυφχψω')); 58 59print(reg2.test('ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ')); 60 61let reg3 =/^[A-Z0-9_\-]*$/i 62print(reg3.test('')) 63 64let reg4 = new RegExp("^(?<urlStrIndex>.*?)(?<urlStr>https?[::]//[^/]+/svn(?:/[a-z0-9.,;?'*:+&%$#=~_ \\u4E00-\\u9FA5-]*)*).*$", "i") 65print(reg4.test('a')); 66 67let reg5 = new RegExp("^(?<urlStrIndex>.*?)(?<urlStr>(?:(?:ht|f)tps?[::]//)?(?:[a-z0-9-]+\\.)+" + "(?:com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|cn|cc|tw|de|au|sg|hk|ei|fr|me|im)(?![a-z])" + "(?:\\:[0-9][0-9]*)?(?:\\.?/[a-z0-9.,;?'\\|*:\\\\+&%$#=~_-]*)*).*$", "i") 68print(reg5.test('a')); 69 70let reg6 = new RegExp("^(?<urlStrIndex>.*?)(?<urlStr>(?:ht|f)tps?[::]//(?:[a-z0-9-]+\\.)*[a-z0-9-]+(?:/[a-z0-9]+)*[/a-z0-9.,;?'\\|*:\\\\+&%$#=~_-]*).*$", "i") 71print(reg6.test('a')); 72 73let reg7 = new RegExp("^(?<urlStrIndex>.*?)(?<urlStr>(?:https?[::]//)?(?:[a-z0-9-\\\\]+\\.)+" + "(?:com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|cn|cc|tw|de|au|sg|hk|ei|fr|me|im)(?![a-z])" + "(?:\\:\\d{4})?(?:/[a-z0-9.,;?'\\|*:\\\\+&%$#=~_-]*)*\\?(?:[a-z0-9]*=[a-z0-9.,;?'*:+%$#=~_\\u4E00-\\u9FA5-]*&?)*).*$", "i") 74print(reg7.test('a')); 75 76let arr = [] 77let temp = true 78var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; 79arr.push(quotedEmailUserUtf8.test(" foo m端ller ")) 80 81let reg8 = /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i 82arr.push(reg8.test('palíndromo')) 83arr.push(reg8.test('órgão')) 84arr.push(reg8.test('qwértyúão')) 85arr.push(reg8.test('àäãcëüïÄÏÜ')) 86 87let reg9 = /^[A-ZÀÉÈÌÎÓÒÙ]+$/i 88arr.push(reg9.test('àéèìîóòù')) 89arr.push(reg9.test('metró')) 90arr.push(reg9.test('pèsca')) 91arr.push(reg9.test('genî')) 92 93let reg10 = /^[A-ZÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$/i 94arr.push(reg10.test('thiến')) 95arr.push(reg10.test('nghiêng')) 96arr.push(reg10.test('chào')) 97arr.push(reg10.test('thế')) 98arr.push(reg10.test('giới')) 99 100let reg11 = /^[A-ZÅÄÖ]+$/i 101arr.push(reg11.test('äiti')) 102 103let reg12 = /^[A-ZÆØÅ]+$/i 104arr.push(reg12.test('aøå')) 105 106let reg13 = /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i 107arr.push(reg13.test('kreską')) 108arr.push(reg13.test('zamknięte')) 109arr.push(reg13.test('zwykłe')) 110arr.push(reg13.test('kropką')) 111arr.push(reg13.test('przyjęły')) 112arr.push(reg13.test('święty')) 113arr.push(reg13.test('Pozwól')) 114 115let reg14 = /^[А-ЯЂЈЉЊЋЏ]+$/i 116arr.push(reg14.test('ШћжЂљЕ')) 117 118let reg15 = /^[A-ZČĆŽŠĐ]+$/i 119arr.push(reg15.test('ŠAabčšđćž')) 120arr.push(reg15.test('ŠATROĆčđš')) 121 122let reg16 = /^[A-ZÁÉÍÑÓÚÜ]+$/i 123arr.push(reg16.test('ábcó')) 124arr.push(reg16.test('dormís')) 125arr.push(reg16.test('volvés')) 126arr.push(reg16.test('español')) 127 128let reg17 = /^[A-ZÅÄÖ]+$/i 129arr.push(reg17.test('religiös')) 130arr.push(reg17.test('stjäla')) 131arr.push(reg17.test('västgöte')) 132 133let reg18 = /^[A-ZÇĞİıÖŞÜ]+$/i 134arr.push(reg18.test('AİıÖöÇ窺ĞğÜüZ')) 135 136let reg19 = /^[Α-ώ]+$/i 137arr.push(reg19.test('άέήίΰϊϋόύώ')) 138arr.push(reg19.test('ΆΈΉΊΪΫΎΏ')) 139 140let reg20 = /^[0-9A-VXYZÇƏĞİıÖŞÜ]+$/i 141arr.push(reg20.test('Azərbaycan')) 142arr.push(reg20.test('abcç2')) 143arr.push(reg20.test('3kərə4kərə')) 144 145let reg21 = /^[0-9А-Я]+$/i 146arr.push(reg21.test('абв1')) 147arr.push(reg21.test('жаба')) 148arr.push(reg21.test('яГоДа2')) 149arr.push(reg21.test('йЮя')) 150 151let reg22 = /^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i 152arr.push(reg22.test('řiť123')) 153 154let reg23 = /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i 155arr.push(reg23.test('1môj')) 156arr.push(reg23.test('2ľúbím')) 157arr.push(reg23.test('3mäkčeň')) 158arr.push(reg23.test('5vŕba')) 159arr.push(reg23.test('6ňorimberk')) 160arr.push(reg23.test('7ťava')) 161arr.push(reg23.test('8žanéta')) 162arr.push(reg23.test('9Ďábelské')) 163arr.push(reg23.test('10ódy')) 164 165let reg24 = /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i 166arr.push(reg24.test('Kán123')) 167arr.push(reg24.test('één354')) 168 169let reg25 = /^[0-9A-ZÅÄÖ]+$/i 170arr.push(reg25.test('äiti124')) 171arr.push(reg25.test('451åå23')) 172 173let reg26 = /^[0-9A-ZÄÖÜß]+$/i 174arr.push(reg26.test('äbc123')) 175 176let reg27 = /^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i 177arr.push(reg27.test('0árvíztűrőtükörfúrógép123')) 178 179let reg28 = /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i 180arr.push(reg28.test('palíndromo')) 181arr.push(reg28.test('2órgão')) 182arr.push(reg28.test('qwértyúão9')) 183arr.push(reg28.test('àäãcë4üïÄÏÜ')) 184 185let reg29 = /^[0-9A-ZÀÉÈÌÎÓÒÙ]+$/i 186arr.push(reg29.test('123àéèìîóòù')) 187arr.push(reg29.test('met23ró')) 188arr.push(reg29.test('pès56ca')) 189arr.push(reg29.test('gen45î')) 190 191let reg30 = /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i 192arr.push(reg30.test('ábcó123')) 193 194let reg31 = /^[0-9A-ZÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$/i 195arr.push(reg31.test('Thầy3')) 196arr.push(reg31.test('3Gà')) 197 198let reg32 = /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i 199arr.push(reg32.test('kre123ską')) 200arr.push(reg32.test('zam21knięte')) 201arr.push(reg32.test('zw23ykłe')) 202arr.push(reg32.test('prz23yjęły')) 203arr.push(reg32.test('świ23ęty')) 204arr.push(reg32.test('Poz1322wól')) 205 206let reg33 = /^[0-9А-ЯЂЈЉЊЋЏ]+$/i 207arr.push(reg33.test('ШћжЂљЕ123')) 208 209let reg34 = /^[0-9A-ZČĆŽŠĐ]+$/i 210arr.push(reg34.test('ŠAabčšđćž123')) 211arr.push(reg34.test('ŠATRO11Ćčđš')) 212 213let reg35 = /^[0-9A-ZÅÄÖ]+$/i 214arr.push(reg35.test('religiös13')) 215arr.push(reg35.test('st23jäla')) 216arr.push(reg35.test('västgöte123')) 217 218let reg36 = /^[0-9A-ZÇĞİıÖŞÜ]+$/i 219arr.push(reg36.test('AİıÖöÇ窺ĞğÜüZ123')) 220 221let reg37 = new RegExp("^[-+]?([0-9]+)?(\\٫[0-9]{1,})?$") 222arr.push(reg37.test('0٫0000000000001')) 223 224let reg38 = new RegExp("^(?:[-+])?(?:[0-9]+)?(?:\\٫[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$") 225arr.push(reg38.test('123٫')) 226arr.push(reg38.test('123٫123')) 227arr.push(reg38.test('-123٫123')) 228 229let reg39 =/^[A-Z0-9_\-]*$/i 230arr.push(reg39.test('')) 231 232let reg40 = RegExp("^(?!-? )(?=.*\\d)(\\¥)?-?(0|[1-9]\\d|[1-9]\\d{0,2}(\\,\\d{3})*)?(\\.(\\d{2}))?$") 233arr.push(reg40.test('¥6,954,231')) 234arr.push(reg40.test('¥-6,954,231')) 235 236var reg41 = /^[A-VXYZÇƏĞİıÖŞÜ]+$/i; 237arr.push(reg41.test('Azərbaycan')) 238arr.push(reg41.test('üöğıəçş')) 239arr.push(reg41.test('sizAzərbaycanlaşdırılmışlardansınızmı')) 240arr.push(reg41.test('dahaBirDüzgünString')) 241arr.push(reg41.test('abcçdeəfgğhxıijkqlmnoöprsştuüvyz')) 242 243let reg42 = /^[А-Я]+$/i 244arr.push(reg42.test('абв')) 245arr.push(reg42.test('жаба')) 246arr.push(reg42.test('яГоДа')) 247 248let reg43 = /^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i 249arr.push(reg43.test('žluťoučký')) 250arr.push(reg43.test('Pěl')) 251arr.push(reg43.test('Ďábelské')) 252arr.push(reg43.test('ódy')) 253 254let reg44 = /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i 255arr.push(reg44.test('môj')) 256arr.push(reg44.test('ľúbím')) 257arr.push(reg44.test('mäkčeň')) 258arr.push(reg44.test('vŕba')) 259arr.push(reg44.test('ňorimberk')) 260 261let reg45 = /^[A-ZÆØÅ]+$/i 262arr.push(reg45.test('aøå')) 263 264let reg46 = /^[A-ZÁÉËÏÓÖÜÚ]+$/i 265arr.push(reg46.test('Kán')) 266arr.push(reg46.test('één')) 267arr.push(reg46.test('vóór')) 268arr.push(reg46.test('nú')) 269arr.push(reg46.test('héél')) 270 271let reg47 = /^[A-ZÄÖÜß]+$/i 272arr.push(reg47.test('äbc')) 273arr.push(reg47.test('FöÖbär')) 274 275let reg48 = /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i 276arr.push(reg48.test('árvíztűrőtükörfúrógép')) 277 278arr.forEach((item)=>{ 279 if(!item){ 280 temp = false 281 } 282}) 283print(temp) 284 285let arr1 = [] 286let temp1 = false 287let reg49 = /[^A-Z0-9+\/=]/i; 288arr1.push(reg49.test("Zg==")); 289arr1.push(reg49.test("Zm8=")); 290arr1.push(reg49.test("Zm9v")); 291arr1.push(reg49.test("Zm9vYg==")); 292arr1.push(reg49.test("Zm9vYmE=")); 293arr1.push(reg49.test("Zm9vYmFy")); 294arr1.push(reg49.test( 295 "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4=" 296)); 297arr1.push(reg49.test("Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg==")); 298arr1.push(reg49.test("U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==")); 299arr1.forEach((item)=>{ 300 if(item){ 301 temp1 = true 302 } 303}) 304print(temp1) 305let str1 = 'SC52BAHL01031234567890123456USD' 306print(str1.replace(/[^A-Z0-9]+/gi, '')) 307 308let reg50 = /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i 309 310let regabc = /abc/g; 311let strabcd = "abcdabcdabcd"; 312for (let i = 0; i < 10; i++) { 313 // cache is used in this case 314 print(regabc.test(strabcd)); 315} 316 317let str2 = "aaaabbBbcccC"; 318for (let i = 0; i < 2; i++) { 319 print(str2); 320 let t1 = str2.replace(/([A-Z])/g, function(e) { 321 return "_" + e; 322 }); 323 print(t1); 324 let t2 = str2.replace(/([A-Z])/g, "_$1"); 325 print(t2); 326 print(t1.replace(/([a-z]+)/g, "_xy")); 327 print(t2.replace(/_/g, "")); 328} 329 330// regexp cache test 331let mediaReg = "\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)\([^\)]+\)"; 332let string = '(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s'; 333const regex1 = new RegExp(mediaReg); 334let matchArray = string.match(regex1); 335print(matchArray); 336 337// Test regexp.test fastpath 338var protoExec = RegExp.prototype.exec 339RegExp.prototype.exec = function () { 340 return null 341} 342var reg = /a/ 343print(reg.test("aaaaa")); 344 345delete RegExp.prototype.exec 346print(reg.test("aaaaa")); 347 348Object.prototype.exec = function () { 349 return null 350} 351print(reg.test("aaaaa")); 352 353delete Object.prototype.exec 354RegExp.prototype.exec = protoExec 355print(reg.test("aaaaa")); 356 357var protoTest = RegExp.prototype.test 358RegExp.prototype.test = function () { 359 return false 360} 361var reg2 = /foo*/ 362print(reg2.test("fooooooo")); 363 364RegExp.prototype.test = protoTest 365print(reg2.test("fooooooo")); 366 367// Same hash in cached result, but different flags. 368var regexp1 = /a*/gs; 369var regexp2 = /a*/g; 370regexp2.lastIndex = 8; 371print(regexp1.exec('aaa')); 372print(regexp2.exec('aaa')); 373 374// Same hash in cached result, and same flags, but different lastIndex. 375var regexp3 = /a*/g; 376var regexp4 = /a*/g; 377regexp4.lastIndex = 1; 378print(regexp3.exec('aaabab')); 379print(regexp4.exec('aaabaa')); 380 381const v43 = /V[\d-\d]/ys; 382const o54 = { 383 __proto__: v43, 384}; 385try { 386 o54.test(Map); 387} catch (e) { 388 print(e) 389} 390 391// Testing regexp.prototype.replace after optimization 392{ 393 const re1 = /[Cz]/; 394 const re2 = /[Cz]/g; 395 const re3 = /([Cz])/; 396 const re4 = /([Cz])/g; 397 398 let replace_str = "abCdefgzabCdefgzabCdefgz"; 399 let replace_result = replace_str.replace(re1, ""); 400 print(re1.lastIndex); 401 let cached_reuslt = replace_str.replace(re1, ""); 402 print(replace_result === cached_reuslt); 403 print(re1.lastIndex); 404 405 replace_result = replace_str.replace(re2, "xyz"); 406 print(re2.lastIndex); 407 cached_reuslt = replace_str.replace(re2, "xyz"); 408 print(replace_result === cached_reuslt); 409 print(re2.lastIndex); 410 411 replace_result = replace_str.replace(re3, "x$1yz"); 412 print(re3.lastIndex); 413 cached_reuslt = replace_str.replace(re3, "x$1yz"); 414 print(replace_result === cached_reuslt); 415 print(re3.lastIndex); 416 417 replace_result = replace_str.replace(re4, String); 418 print(re4.lastIndex); 419 cached_reuslt = replace_str.replace(re4, String); 420 print(replace_result === cached_reuslt); 421 print(re4.lastIndex); 422} 423 424// test RegExp.prototype.xx 425print(RegExp.prototype.dotAll) 426print(RegExp.prototype.global) 427print(RegExp.prototype.hasIndices) 428print(RegExp.prototype.ignoreCase) 429print(RegExp.prototype.multiline) 430print(RegExp.prototype.sticky) 431print(RegExp.prototype.unicode) 432print(RegExp.prototype.lastIndex) 433print(RegExp.prototype.flags) 434print(RegExp.prototype.source) 435try { 436 RegExp.prototype.test("abc") 437} catch (e) { 438 print(e.name) 439} 440try { 441 RegExp.prototype.exec("abc") 442} catch (e) { 443 print(e.name) 444} 445print(RegExp.prototype.toString()) 446 447let inputString = "/vedio/av{avid}{cid}"; 448let extractedContent = inputString.match(/\{([^{}]+)\}/g); 449let replacedString = inputString.replace(/\{([^{}]+)\}/g, '(uuu)').replace(/\//g, "\\/"); 450print(replacedString); 451 452let str = "beep boop afff测试样本one1"; 453print(str.split(/([{}:;,]|\s+)/)); 454 455function verifyRegExp(text) { 456 text = text.replace(new RegExp('[\\s]', 'g'), ' '); 457 let emRegExp = new RegExp('<em>.*?</em>', 'ig'); 458 let results = text.split(emRegExp); 459 text.match(emRegExp); 460 return results; 461} 462 463const srcTextA = '<em>a</em>bcdefghijklmnopqrstuvwxyz<em>a</em>bcdefghijklmnopqrstuvwxyz<em>a</em>bcdefghijklmnopqrstuvwxyz<em>a</em>bcdefghijklmnopqrstuvwxyz' 464const srcTextAbcd = '<em>abcd</em>efghijklmnopqrstuvwxyz<em>abcd</em>efghijklmnopqrstuvwxyz<em>abcd</em>efghijklmnopqrstuvwxyz<em>abcd</em>efghijklmnopqrstuvwxyz' 465 466print(`verifyRegExp_1: ${verifyRegExp(srcTextA)}`); 467print(`verifyRegExp_2: ${verifyRegExp(srcTextAbcd)}`); 468print(`verifyRegExp_3: ${verifyRegExp(srcTextAbcd)}`); 469 470//regexp unicode property 471const ans1 = /\p{Alphabetic}/u.test("æ"); 472const ans2 = /\p{Alpha}/u.test("2"); 473const ans3 = /^[\p{Lu}\p{Ll}]+$/u.test("ASDFasdf"); 474const ans4 = /^[\P{Lu}\p{Ll}]+$/u.test("ASDFasdf"); 475const ans5 = /\P{Ll}/iu.test("b"); 476const ans6 = /\p{Ll}/iu.test("b"); 477const ans7 = /[^\P{Any}]+/u.test(456); 478const ans8 = /\p{Assigned}+/u.test(""); 479const ans9 = /[\p{P}\p{S}]/u.test("!"); 480const ans10 = /\p{General_Category=Math_Symbol}/u.test("+"); 481print(ans1); 482print(ans2); 483print(ans3); 484print(ans4); 485print(ans5); 486print(ans6); 487print(ans7); 488print(ans8); 489print(ans9); 490print(ans10); 491try { 492 const ans11 = RegExp("/[\\p{}]/u"); 493 print(ans11); 494} catch(e) { 495 print(e); 496} 497const str3 = "a-b-c"; 498const re = /-/y; 499print(str3.split(re)); 500 501re.lastIndex = 1; 502print(str3.split(re)); 503 504re.lastIndex = -1; 505print(str3.split(re)); 506 507re.lastIndex = 3; 508print(str3.split(re)); 509 510print(re.test(str3)); 511 512print(str3.split(/-/g)); 513 514// search 515const str4 = "abc"; 516let re1 = /b/; 517re1.lastIndex = 2; 518print(str4.search(re1)); 519print(str4.search(/b/y)); 520print(str4.search(re1)); 521print(re1.lastIndex); 522 523// check cache 524const str5 = "a-bc"; 525let re2 = /-/; 526re2.lastIndex = 2; 527print(str5.split(re2)); 528print(re2.lastIndex); 529print(str5.split(re2)); 530print(re2.lastIndex); 531 532const str6 = "abcabc"; 533let re3 = /abc/; 534re3.lastIndex = 2; 535print(str6.match(re3)); 536print(re3.lastIndex); 537print(str6.match(re3)); 538print(re3.lastIndex); 539 540let re4 = /abc/g; 541re4.lastIndex = 2; 542print(str6.match(re4)); 543print(re4.lastIndex); 544print(str6.match(re4)); 545print(re4.lastIndex); 546Object.defineProperty(RegExp.prototype, "global", { 547 value: true 548}) 549var flags = RegExp.prototype.flags; 550Object.defineProperty(RegExp.prototype, "global", { 551 value: false 552}) 553print(flags); 554print(str6.match(re4)); 555print(re4.lastIndex); 556print(str6.match(re4)); 557print(re4.lastIndex); 558 559let myExp = new RegExp("a+b+c"); 560Object.defineProperty(myExp, "sticky", { 561 value: true 562 }) 563print(myExp.toString()); 564 565// Testing regexp.prototype.replace after optimization 566{ 567 const re1 = /[Cz]/; 568 const re2 = /[Cz]/g; 569 const re3 = /([Cz])/; 570 const re4 = /([Cz])/g; 571 572 let replace_str = "abCdefgzabCdefgzabCdefgz"; 573 let replace_result = replace_str.replace(re1, ""); 574 print(re1.lastIndex); 575 let cached_reuslt = replace_str.replace(re1, ""); 576 print(replace_result === cached_reuslt); 577 print(re1.lastIndex); 578 579 replace_result = replace_str.replace(re2, "xyz"); 580 print(re2.lastIndex); 581 cached_reuslt = replace_str.replace(re2, "xyz"); 582 print(replace_result === cached_reuslt); 583 print(re2.lastIndex); 584 585 replace_result = replace_str.replace(re3, "x$1yz"); 586 print(re3.lastIndex); 587 cached_reuslt = replace_str.replace(re3, "x$1yz"); 588 print(replace_result === cached_reuslt); 589 print(re3.lastIndex); 590 591 replace_result = replace_str.replace(re4, String); 592 print(re4.lastIndex); 593 cached_reuslt = replace_str.replace(re4, String); 594 print(replace_result === cached_reuslt); 595 print(re4.lastIndex); 596} 597 598const regex = /(?:)+/; 599const str10 = "abcabcabc"; 600const matches = regex.test(str10); 601print(matches); 602const matches1 = regex.exec(str10); 603print(matches1); 604 605try { 606 let matchReg = new RegExp("@【哈哈】*^o^*|@小米(kk)",'g'); 607} catch (error) { 608 print(error) 609} 610 611let e = /./; 612e.exec = function() { 613 return []; 614} 615"".replace(e, ""); 616delete e.exec; 617 618{ 619 const v0 = /qeu(?<a>.)\k<a>(x)(x)(x)\1*xyz{93}/ugysd; 620 const v4 = typeof Date === "string"; 621 v0[Symbol.match] = v4; 622 print(v0["exec"]()); 623} 624 625{ 626 ("65535").split(/(?!\1(a\1)\1)\1/g); 627 const o4 = { 628 ...RegExp, 629 }; 630 print(o4); 631} 632 633{ 634 const v2 = /e\8Z(x)(x)(x)(x)(x)(x)(x)(x)(x)(x)\10*/misd; 635 v2[1073741824] = -194290175n; 636 for (let v3 = 0; v3 < 2; v3++) { 637 print(v2.test(-194290175n)); 638 } 639} 640 641function f(...args) { 642 return {}; 643} 644let reg51 = new RegExp("a"); 645f.exec = f; 646let relpfun = reg51[Symbol.replace]; 647relpfun.apply(f, [1, 2, 3, 4]); 648print("success"); 649 650{ 651 let str = /^\s*([^;\s]*)/; 652 str.test("text/html"); 653 print(RegExp.$1); 654 str.test("text/plain"); 655 print(RegExp.$1); 656 str.test("text/html"); 657 print(RegExp.$1); 658} 659 660{ 661 let reg52 = /abc/; 662 let count = 0; 663 print(reg52.ignoreCase); 664 print(reg52.global); 665 print(reg52.flags); 666 Object.defineProperty(reg52, "global", { 667 get: function() { count++; return true; } 668 }); 669 Object.defineProperty(reg52, "ignoreCase", { 670 get: function() { count++; return true; } 671 }); 672 673 print(reg52.ignoreCase); 674 print(count); 675 print(reg52.global); 676 print(count); 677 print(reg52.flags); 678 print(count); 679} 680 681// The above test case in false environment did not reset the environment 682Object.defineProperty(RegExp.prototype, "global", { 683 value: true 684}) 685string = 'aaa\n789\r\nccc\r\n345'; 686var pattern = /\d$/gm; 687result = string.match(pattern); 688print(2 == result.length); 689print('9' == result[0]); 690print('5' == result[1]); 691 692string = 'aaa\n789\r\nccc\r\nddd'; 693pattern = /\d$/gm; 694result = string.match(pattern); 695print(1 == result.length); 696print('9' == result[0]); 697 698// test getFlags 699Object.defineProperty(RegExp.prototype, "global", { 700 value: true 701}) 702const res = /abc/giymsud; 703res.lastIndex = -1; 704print(res.flags); 705const res1 = /abc/g; 706res1.lastIndex = -1; 707print(res1.flags); 708const res2 = /abc/i; 709res2.lastIndex = -1; 710print(res2.flags); 711const res3 = /abc/y; 712res3.lastIndex = -1; 713print(res3.flags); 714const res4 = /abc/m; 715res4.lastIndex = -1; 716print(res4.flags); 717const res5 = /abc/s; 718res5.lastIndex = -1; 719print(res5.flags); 720const res6 = /abc/u; 721res6.lastIndex = -1; 722print(res6.flags); 723const res7 = /abc/d; 724res7.lastIndex = -1; 725print(res7.flags); 726const res8 = /abc/; 727res8.lastIndex = -1; 728print(res8.flags); 729 730{ 731 let str="\\\\\\\\[aaa" 732 try { 733 let pattern="["; 734 let reg = new RegExp(pattern); 735 print(pattern,JSON.stringify(reg.exec(str))) 736 } catch (e) { 737 print(e.name); 738 } 739 try { 740 let pattern="\["; 741 let reg = new RegExp(pattern); 742 print(pattern,JSON.stringify(reg.exec(str))) 743 } catch (e) { 744 print(e.name); 745 } 746 try { 747 let pattern="\\["; 748 let reg = new RegExp(pattern); 749 print(pattern,JSON.stringify(reg.exec(str))) 750 } catch (e) { 751 print(e.name); 752 } 753 try { 754 let pattern="\\\["; 755 let reg = new RegExp(pattern); 756 print(pattern,JSON.stringify(reg.exec(str))) 757 } catch (e) { 758 print(e.name); 759 } 760 try { 761 let pattern="\\\\["; 762 let reg = new RegExp(pattern); 763 print(pattern,JSON.stringify(reg.exec(str))) 764 } catch (e) { 765 print(e.name); 766 } 767 try { 768 let pattern="\\\\\["; 769 let reg = new RegExp(pattern); 770 print(pattern,JSON.stringify(reg.exec(str))) 771 } catch (e) { 772 print(e.name); 773 } 774 try { 775 let pattern="\\\\\\["; 776 let reg = new RegExp(pattern); 777 print(pattern,JSON.stringify(reg.exec(str))) 778 } catch (e) { 779 print(e.name); 780 } 781 try { 782 let pattern="\\\\\\\["; 783 let reg = new RegExp(pattern); 784 print(pattern,JSON.stringify(reg.exec(str))) 785 } catch (e) { 786 print(e.name); 787 } 788} 789 790{ 791 Object.defineProperty(RegExp.prototype, "global", { 792 value: false 793 }) 794 let str1; 795 let result; 796 const re1 = /[Cz]/; 797 const re2 = /[Cz]/g; 798 const re3 = /([Cz])/; 799 const re4 = /([Cz])/g; 800 801 function createHaystack() { 802 let s = "abCdefgz"; 803 for (let i = 0; i < 3; i++) s += s; 804 return s; 805 } 806 str1 = createHaystack(); 807 function String1Replace(re) { 808 result = re[Symbol.replace](str1, "."); 809 } 810 function String2Replace(re) { 811 result = re[Symbol.replace](str1, "xyz"); 812 } 813 function String3Replace(re) { 814 result = re[Symbol.replace](str1, "x$1yz"); 815 } 816 function Function1Replace(re) { 817 result = re[Symbol.replace](str1, String); 818 } 819 String1Replace(re1); 820 print(result); 821 String1Replace(re2); 822 print(result); 823 String2Replace(re2); 824 print(result); 825 String2Replace(re1); 826 print(result); 827 String3Replace(re3); 828 print(result); 829 String3Replace(re4); 830 print(result); 831 Function1Replace(re3); 832 print(result); 833 Function1Replace(re4); 834 print(result); 835 836 // subStringIsUtf8 branch canbecompressed 837 str1 = 'utf83c这要替换"!'; 838 let regexp = /([0-9])([a-z])/g 839 let newStr1 = str1.replace(regexp, "$1" ); 840 print(newStr1); 841 842 // subStringIsUtf8 branch length=0; 843 str1 = '3c这要替换"!'; 844 regexp = /([0-9])([a-z])/g 845 newStr1 = str1.replace(regexp, "$1" ); 846 print(newStr1); 847 848 // subStringIsUtf8 branch src isUtf8; 849 str1 = 'abcdefg3chigk"!'; 850 regexp = /([0-9])([a-z])/g 851 newStr1 = str1.replace(regexp, "$1" ); 852 print(newStr1); 853} 854 855{ 856 let s1 = 'abc'; 857 let s2 = '\ud834\udf06'; 858 let reg = new RegExp(s1.repeat(10000)); 859 let str = s1.repeat(10000); 860 let res = str.replace(reg, s2); 861 print(res == s2); 862 863 reg = new RegExp(s2.repeat(10000)); 864 str = s2.repeat(10000); 865 res = str.replace(reg, s1); 866 print(res == s1); 867} 868