1/* 2 * Copyright (c) 2024 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:stringreplaceall 18 * @tc.desc:test stringreplaceall 19 * @tc.type: FUNC 20 * @tc.require: issueIATECS 21 */ 22 23// case1 - string: utf8, search: not regexp, replace: no dollar 24let str1 = "Hello, world"; 25let res1 = str1.replaceAll("o", "o-o"); 26assert_equal(res1,"Hello-o, wo-orld"); 27 28// case2 - string: utf8, search: regexp, replace: no dollar 29let res2 = str1.replaceAll(/o/g, "o-o"); 30assert_equal(res2,"Hello-o, wo-orld"); 31 32// case3 - string: utf8, search: regexp, replace: dollar 33let res3 = str1.replaceAll(/o/g, "$&-$&"); 34assert_equal(res3,"Hello-o, wo-orld"); 35 36// case4 - string: utf8, search: not regexp, replace: dollar 37let res4 = str1.replaceAll("o", "$&-$&"); 38assert_equal(res4,"Hello-o, wo-orld"); 39 40// case5 - string: utf8, search: not regexp, replace: function 41let res5 = str1.replaceAll("o", String); 42assert_equal(res5,"Hello, world"); 43 44// case6 - string: utf16, search: not regexp, replace: no dollar 45let str3 = "你好,世界!你好,世界!"; 46let res6 = str3.replaceAll("好", "好-好"); 47assert_equal(res6,"你好-好,世界!你好-好,世界!"); 48 49// case7 - string: utf16, search: regexp, replace: no dollar 50let res7 = str3.replaceAll(/好/g, "好-好"); 51assert_equal(res7,"你好-好,世界!你好-好,世界!"); 52 53// case8 - string: utf16, search: regexp, replace: dollar 54let res8 = str3.replaceAll(/好/g, "$&-$&"); 55assert_equal(res8,"你好-好,世界!你好-好,世界!"); 56 57// case9 - string: utf16, search: not regexp, replace: dollar 58let res9 = str3.replaceAll("好", "$&-$&"); 59assert_equal(res9,"你好-好,世界!你好-好,世界!"); 60 61// case10 - string: utf16, search: not regexp, replace: function 62let res10 = str3.replaceAll("好", String); 63assert_equal(res10,"你好,世界!你好,世界!"); 64 65test_end();