Hướng dẫn replace all character in string javascript - thay thế tất cả ký tự trong chuỗi javascript

Tôi chỉ mã hóa một điểm chuẩn và kiểm tra 3 câu trả lời đầu tiên. Có vẻ như đối với các chuỗi ngắn (
the third most voted answer is faster than the second most voted one.

Đối với các chuỗi dài (thêm ".repeat (300)" vào chuỗi thử nghiệm), câu trả lời nhanh hơn 1 sau đó là lần thứ hai và thứ ba.

Note:

Trên đây là đúng đối với các trình duyệt sử dụng động cơ V8 (Chrome/Chromium, v.v.). Với Firefox (động cơ Spidermonkey), kết quả hoàn toàn khác nhau kiểm tra cho chính mình !! Firefox với giải pháp thứ ba dường như nhanh hơn 4,5 lần so với Chrome với giải pháp đầu tiên ... Crazy: D
With firefox (SpiderMonkey engine) the results are totally different
Check for yourselves!! Firefox with the third solution seems to be
more than 4.5 times faster than Chrome with the first solution... crazy :D

function log(data) {
  document.getElementById("log").textContent += data + "\n";
}

benchmark = (() => {

  time_function = function(ms, f, num) {
    var z;
    var t = new Date().getTime();
    for (z = 0;
      ((new Date().getTime() - t) < ms); z++) f(num);
    return (z / ms)
  } // returns how many times the function was run in "ms" milliseconds.


  function benchmark() {
    function compare(a, b) {
      if (a[1] > b[1]) {
        return -1;
      }
      if (a[1] < b[1]) {
        return 1;
      }
      return 0;
    }

    // functions

    function replace1(s) {
      s.replace(/foo/g, "bar")
    }

String.prototype.replaceAll2 = function(_f, _r){ 

  var o = this.toString();
  var r = '';
  var s = o;
  var b = 0;
  var e = -1;
//      if(_c){ _f = _f.toLowerCase(); s = o.toLowerCase(); }

  while((e=s.indexOf(_f)) > -1)
  {
    r += o.substring(b, b+e) + _r;
    s = s.substring(e+_f.length, s.length);
    b += e+_f.length;
  }

  // Add Leftover
  if(s.length>0){ r+=o.substring(o.length-s.length, o.length); }

  // Return New String
  return r;
};

String.prototype.replaceAll = function(str1, str2, ignore) {
      return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
    }

    function replace2(s) {
      s.replaceAll("foo", "bar")
    }

    function replace3(s) {
      s.split('foo').join('bar');
    }

    function replace4(s) {
      s.replaceAll2("foo", "bar")
    }


    funcs = [
      [replace1, 0],
      [replace2, 0],
      [replace3, 0],
      [replace4, 0]
    ];

    funcs.forEach((ff) => {
      console.log("Benchmarking: " + ff[0].name);
      ff[1] = time_function(2500, ff[0], "foOfoobarBaR barbarfoobarf00".repeat(10));
      console.log("Score: " + ff[1]);

    })
    return funcs.sort(compare);
  }

  return benchmark;
})()
log("Starting benchmark...\n");
res = benchmark();
console.log("Winner: " + res[0][0].name + " !!!");
count = 1;
res.forEach((r) => {
  log((count++) + ". " + r[0].name + " score: " + Math.floor(10000 * r[1] / res[0][1]) / 100 + ((count == 2) ? "% *winner*" : "% speed of winner.") + " (" + Math.round(r[1] * 100) / 100 + ")");
});
log("\nWinner code:\n");
log(res[0][0].toString());

Bài kiểm tra sẽ chạy trong 10 giây (+2) khi bạn nhấp vào nút.

Kết quả của tôi (trên cùng một PC):

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)

Mess mess uh?

Đã tự do thêm nhiều kết quả kiểm tra

Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)

Chrome trên Galaxy Note 4

1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)

Không có cách nào dễ dàng để thay thế tất cả các lần xuất hiện chuỗi trong JavaScript. Java, đã phục vụ một nguồn cảm hứng cho JavaScript trong những ngày đầu tiên, có phương pháp

4 trên các chuỗi kể từ năm 1995!

Trong bài đăng này, bạn sẽ học cách thay thế tất cả các lần xuất hiện chuỗi trong JavaScript bằng cách chia và nối một chuỗi và

5 kết hợp với biểu thức chính quy toàn cầu.

Hơn nữa, bạn sẽ đọc về chuỗi đề xuất mới.replaceall () (ở giai đoạn 4) mang lại tất cả các phương thức cho các chuỗi JavaScript. Đây là cách tiếp cận thuận tiện nhất.

1. Tách và tham gia một mảng

Nếu bạn Google làm thế nào để "thay thế tất cả các lần xuất hiện chuỗi trong JavaScript", rất có thể phương pháp đầu tiên bạn tìm thấy là sử dụng một mảng trung gian.

Đây là cách nó hoạt động:

  1. Chia
    6 thành
    7 cho chuỗi
    8:

javascript

const pieces = string.split(search);

  1. Sau đó tham gia các phần đặt chuỗi
    9 ở giữa:

javascript

const resultingString = pieces.join(replace);

Ví dụ: chúng ta hãy thay thế tất cả các khoảng trống

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
0 bằng dấu gạch nối
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1 trong chuỗi
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
2:

javascript

const search = ' ';

const replaceWith = '-';

const result = 'duck duck go'.split(search).join(replaceWith);

result; // => 'duck-duck-go'

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
3 chia chuỗi thành các mảnh:
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
4.

Sau đó, các mảnh

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
5 được nối bằng cách chèn
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1 ở giữa chúng, dẫn đến chuỗi
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
7.

Đây là một chức năng trợ giúp tổng quát sử dụng phương pháp phân tách và tham gia:

javascript

function replaceAll(string, search, replace) {

return string.split(search).join(replace);

}

replaceAll('abba', 'a', 'i'); // => 'ibbi'

replaceAll('go go go!', 'go', 'move'); // => 'move move move!'

replaceAll('oops', 'z', 'y'); // => 'oops'

Cách tiếp cận này yêu cầu chuyển đổi chuỗi thành một mảng, và sau đó quay lại thành một chuỗi. Hãy tiếp tục tìm kiếm các lựa chọn thay thế tốt hơn.

2. Thay thế () bằng biểu thức chính quy toàn cầu

Phương thức chuỗi

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
8 tìm kiếm và thay thế các lần xuất hiện của biểu thức chính quy
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
9 bằng chuỗi
Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
0.

Để thực hiện phương thức

Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
1 Thay thế tất cả các lần xuất hiện của mẫu, bạn phải kích hoạt cờ toàn cầu trên biểu thức thông thường:

  1. Nối
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    2 Sau khi kết thúc biểu thức thông thường theo nghĩa đen:
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    3
  2. Hoặc khi sử dụng hàm tạo biểu thức chính quy, hãy thêm
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    4 vào đối số thứ hai:
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    5

Hãy thay thế tất cả các lần xuất hiện của

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
0 bằng
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1:

javascript

const searchRegExp = /\s/g;

const replaceWith = '-';

const result = 'duck duck go'.replace(searchRegExp, replaceWith);

result; // => 'duck-duck-go'

Biểu thức thông thường theo nghĩa đen

Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
8 (lưu ý cờ toàn cầu
Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
2) khớp với không gian
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
0.

1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
1 thay thế tất cả các trận đấu của
Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
8 bằng
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1, dẫn đến
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
7.

Bạn có thể dễ dàng thay thế trường hợp không nhạy cảm bằng cách thêm cờ

1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
5 vào biểu thức thông thường:

0

Biểu thức thông thường

1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
6 thực hiện tìm kiếm không nhạy cảm với trường hợp toàn cầu (lưu ý
1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
5 và
Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
2 cờ).
1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
6 khớp với

javascript

const pieces = string.split(search);

0, cũng như

javascript

const pieces = string.split(search);

1.

Gọi

javascript

const pieces = string.split(search);

2 thay thế tất cả các trận đấu của các chuỗi con
1. replace4 score: 100% *winner* (99.82)
2. replace1 score: 91.04% speed of winner. (90.88)
3. replace3 score: 70.27% speed of winner. (70.15)
4. replace2 score: 38.25% speed of winner. (38.18)
6 với

javascript

const pieces = string.split(search);

4.

2.1 Biểu thức chính quy từ chuỗi

Khi biểu thức chính quy được tạo từ một chuỗi, bạn phải thoát khỏi các ký tự

javascript

const pieces = string.split(search);

5 vì chúng có ý nghĩa đặc biệt trong biểu thức thông thường.

Do đó, các ký tự đặc biệt là một vấn đề khi bạn muốn thay thế tất cả các hoạt động. Đây là một ví dụ:

1

Đoạn trích ở trên cố gắng chuyển đổi chuỗi tìm kiếm

javascript

const pieces = string.split(search);

6 thành một biểu thức thông thường. Nhưng

javascript

const pieces = string.split(search);

6 là một biểu thức chính quy không hợp lệ, do đó

javascript

const pieces = string.split(search);

8 bị ném.

Thoát khỏi nhân vật

javascript

const pieces = string.split(search);

9 giải quyết vấn đề.

Tuy nhiên, nó có đáng để thoát khỏi chuỗi tìm kiếm bằng cách sử dụng một hàm như escaperegexp () để được sử dụng làm biểu thức thông thường không? Hầu như không.

2.2 thay thế () bằng một chuỗi

Nếu đối số đầu tiên

8 của

javascript

const resultingString = pieces.join(replace);

1 là một chuỗi, thì phương thức chỉ thay thế lần xuất hiện đầu tiên của
8:

2

javascript

const resultingString = pieces.join(replace);

3 chỉ thay thế sự xuất hiện đầu tiên của một không gian.

3. Phương thức thay thế ()

Cuối cùng, phương thức

javascript

const resultingString = pieces.join(replace);

4 thay thế tất cả các lần xuất hiện của chuỗi
8 bằng
Chrome/Windows 10
1. replace1 score: 100% *winner* (742.49)
2. replace4 score: 85.58% speed of winner. (635.44)
3. replace2 score: 54.42% speed of winner. (404.08)
4. replace3 score: 50.06% speed of winner. (371.73)

Firefox/Windows 10
1. replace3 score: 100% *winner* (2645.18)
2. replace1 score: 30.77% speed of winner. (814.18)
3. replace4 score: 22.3% speed of winner. (589.97)
4. replace2 score: 12.51% speed of winner. (331.13)

Edge/Windows 10
1. replace1 score: 100% *winner* (1251.24)
2. replace2 score: 46.63% speed of winner. (583.47)
3. replace3 score: 44.42% speed of winner. (555.92)
4. replace4 score: 20% speed of winner. (250.28)
0.

Hãy thay thế tất cả các lần xuất hiện của

Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
0 bằng
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1:

3

javascript

const resultingString = pieces.join(replace);

9 Thay thế tất cả các lần xuất hiện của chuỗi
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
0 bằng
Chrome/Linux Ubuntu 64:
1. replace1 score: 100% *winner* (766.18)
2. replace4 score: 99.07% speed of winner. (759.11)
3. replace3 score: 68.36% speed of winner. (523.83)
4. replace2 score: 59.35% speed of winner. (454.78)

Firefox/Linux Ubuntu 64
1. replace3 score: 100% *winner* (3480.1)
2. replace1 score: 13.06% speed of winner. (454.83)
3. replace4 score: 9.4% speed of winner. (327.42)
4. replace2 score: 4.81% speed of winner. (167.46)
1.

javascript

const resultingString = pieces.join(replace);

4 là cách tốt nhất để thay thế tất cả các chuỗi xuất hiện trong một chuỗi

Lưu ý rằng hiện tại, hỗ trợ phương thức trong trình duyệt bị hạn chế và bạn có thể yêu cầu polyfill.

3.1 Sự khác biệt giữa thay thế () và thay thế ()

Các phương thức chuỗi

javascript

const search = ' ';

const replaceWith = '-';

const result = 'duck duck go'.split(search).join(replaceWith);

result; // => 'duck-duck-go'

3 và

javascript

const search = ' ';

const replaceWith = '-';

const result = 'duck duck go'.split(search).join(replaceWith);

result; // => 'duck-duck-go'

4 hoạt động theo cùng một cách, mong đợi 2 điều:

  1. Nếu đối số
    8 là một chuỗi,
    4 thay thế tất cả các lần xuất hiện của
    8 bằng
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    0, trong khi
    Chrome/Windows 10
    1. replace1 score: 100% *winner* (742.49)
    2. replace4 score: 85.58% speed of winner. (635.44)
    3. replace2 score: 54.42% speed of winner. (404.08)
    4. replace3 score: 50.06% speed of winner. (371.73)
    
    Firefox/Windows 10
    1. replace3 score: 100% *winner* (2645.18)
    2. replace1 score: 30.77% speed of winner. (814.18)
    3. replace4 score: 22.3% speed of winner. (589.97)
    4. replace2 score: 12.51% speed of winner. (331.13)
    
    Edge/Windows 10
    1. replace1 score: 100% *winner* (1251.24)
    2. replace2 score: 46.63% speed of winner. (583.47)
    3. replace3 score: 44.42% speed of winner. (555.92)
    4. replace4 score: 20% speed of winner. (250.28)
    
    1 chỉ xảy ra lần đầu tiên
  2. Nếu đối số
    8 là một biểu thức thông thường không toàn cầu, thì
    4 sẽ ném một ngoại lệ

    javascript

    function replaceAll(string, search, replace) {

    return string.split(search).join(replace);

    }

    replaceAll('abba', 'a', 'i'); // => 'ibbi'

    replaceAll('go go go!', 'go', 'move'); // => 'move move move!'

    replaceAll('oops', 'z', 'y'); // => 'oops'

    2.

4. Key Takeaway

Cách tiếp cận nguyên thủy để thay thế tất cả các lần xuất hiện là chia chuỗi thành các đoạn theo chuỗi tìm kiếm, nối lại chuỗi đặt chuỗi thay thế giữa các khối:

javascript

function replaceAll(string, search, replace) {

return string.split(search).join(replace);

}

replaceAll('abba', 'a', 'i'); // => 'ibbi'

replaceAll('go go go!', 'go', 'move'); // => 'move move move!'

replaceAll('oops', 'z', 'y'); // => 'oops'

3. Cách tiếp cận này hoạt động, nhưng nó hacky.

Một cách tiếp cận khác là sử dụng

javascript

function replaceAll(string, search, replace) {

return string.split(search).join(replace);

}

replaceAll('abba', 'a', 'i'); // => 'ibbi'

replaceAll('go go go!', 'go', 'move'); // => 'move move move!'

replaceAll('oops', 'z', 'y'); // => 'oops'

4 với biểu thức thông thường có cờ toàn cầu được bật.

Thật không may, bạn không thể dễ dàng tạo các biểu thức chính quy từ một chuỗi trong thời gian chạy, bởi vì các ký tự đặc biệt của các biểu thức chính quy phải được thoát ra. Và đối phó với một biểu thức thường xuyên cho một sự thay thế đơn giản của các chuỗi là quá sức.

Cuối cùng, phương thức chuỗi mới

javascript

const resultingString = pieces.join(replace);

4 thay thế tất cả các lần xuất hiện chuỗi. Phương pháp này là một đề xuất ở Giai đoạn 4, và hy vọng, nó sẽ sớm đạt được một tiêu chuẩn JavaScript mới.

Đề xuất của tôi là sử dụng

javascript

function replaceAll(string, search, replace) {

return string.split(search).join(replace);

}

replaceAll('abba', 'a', 'i'); // => 'ibbi'

replaceAll('go go go!', 'go', 'move'); // => 'move move move!'

replaceAll('oops', 'z', 'y'); // => 'oops'

6 để thay thế các chuỗi.

Những cách khác để thay thế tất cả các lần xuất hiện chuỗi mà bạn biết? Hãy chia sẻ một bình luận dưới đây!

Làm thế nào để bạn thay thế tất cả các lần xuất hiện của một ký tự trong một chuỗi trong JavaScript?

Để làm cho phương thức thay thế () thay thế tất cả các lần xuất hiện của mẫu, bạn phải kích hoạt cờ toàn cầu trên biểu thức thông thường: nối g sau khi kết thúc biểu thức thông thường theo nghĩa đen: /search /g. Hoặc khi sử dụng hàm tạo biểu thức chính quy, hãy thêm 'g' vào đối số thứ hai: regexp mới ('search', 'g'))Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')

Làm thế nào để bạn thay thế tất cả các chữ cái trong một chuỗi?

Để thay thế tất cả các lần xuất hiện của một chuỗi con trong chuỗi bằng một chuỗi mới, bạn có thể sử dụng phương thức thay thế () hoặc thay thế (): thay thế (): Biến phần phụ thành biểu thức chính quy và sử dụng cờ G. Phương thức thay thế () phương pháp thẳng tiến hơn.use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.

Làm thế nào để bạn thay thế tất cả các lần xuất hiện của một từ trong một chuỗi trong javascript?

String.Prototype.RepLaceAll () Phương thức thay thế () Phương thức trả về một chuỗi mới với tất cả các kết quả của một mẫu được thay thế bằng cách thay thế.Mẫu có thể là một chuỗi hoặc một regexp, và sự thay thế có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu. The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.

Làm thế nào để bạn thay thế một ký tự trong một chuỗi trong javascript mà không cần sử dụng phương thức thay thế ()?

Bạn có thể sử dụng kết hợp chia tách và tham gia để đạt được một cách đơn giản.