From 8cff243d6bbf20ce045ba01c12706ebfe2dba353 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Mon, 11 Mar 2024 12:26:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Estring.match=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/String.uts | 40 ++++++++++++++----------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/String.uts b/uni_modules/uts-tests/utssdk/String.uts index 602a293..4c21c6f 100644 --- a/uni_modules/uts-tests/utssdk/String.uts +++ b/uni_modules/uts-tests/utssdk/String.uts @@ -112,27 +112,31 @@ export function testString(): Result { expect("".indexOf("test")).toEqual(-1); }) - // test('match', () => { - // const str = 'The quick brown fox jumps over the lazy dog. It barked.'; - // const result = str.match(new RegExp('[A-Z]', 'g')); - // expect(result).toEqual(["T", "I"]); + test('match', () => { + const str = 'The quick brown fox jumps over the lazy dog. It barked.'; + const result = str.match(new RegExp('[A-Z]', 'g')); + expect(result!!.length).toEqual(2); + expect(result!![0]).toEqual("T"); + expect(result!![1]).toEqual("I"); - // const str1 = 'For more information, see Chapter 3.4.5.1'; - // const result1 = str1.match(/see (chapter \d+(\.\d)*)/i); - // expect(result1[0]).toEqual("see Chapter 3.4.5.1"); - // expect(result1[1]).toEqual("Chapter 3.4.5.1"); - // expect(result1[2]).toEqual(".1"); - // expect(result1.index).toEqual(22); - // expect(result1.input).toEqual(str1); + const result2 = str.match(new RegExp('[A-Z]')); + expect(result2!!.length).toEqual(1); + expect(result2!![0]).toEqual("T"); - // const str2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; - // const result2 = str2.match(/[A-E]/gi); - // expect(result2).toEqual(['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']); + const gradientString = 'linear-gradient(to right, rgb(255, 0, 0), #00FF00, hsl(120, 100%, 50%))'; + const pattern = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|#([a-fA-F0-9]{2}){3}|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)/g; + const result3 = gradientString.match(pattern); + expect(result3!!.length).toEqual(3); + expect(result3!![0]).toEqual("rgb(255, 0, 0)"); + expect(result3!![2]).toEqual("hsl(120, 100%, 50%)"); - // const str3 = 'Nothing will come of nothing.'; - // const result3 = str3.match(); - // expect(result3).toEqual([""]); - // }) + const pattern2 = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|#([a-fA-F0-9]{2}){3}|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)/; + const result4 = gradientString.match(pattern2); + expect(result4!!.length).toEqual(8); + expect(result4!![0]).toEqual("rgb(255, 0, 0)"); + expect(result4!![1]).toEqual("255"); + expect(result4!![2]).toEqual("0"); + }) test('padEnd', () => { const str1 = 'Breaded Mushrooms'; expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........"); -- GitLab