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

Ví dụ

Thay thế Microsoft:

Đặt văn bản = "Truy cập Microsoft!"; Đặt kết quả = text.replace ("Microsoft", "W3Schools");
let result = text.replace("Microsoft", "W3Schools");

Hãy tự mình thử »

Thay thế toàn cầu:

hãy để văn bản = "Mr Blue có một ngôi nhà màu xanh và một chiếc xe màu xanh"; Đặt kết quả = text.replace (/blue/g, "màu đỏ");
let result = text.replace(/blue/g, "red");

Hãy tự mình thử »

Thay thế toàn cầu:


hãy để văn bản = "Mr Blue có một ngôi nhà màu xanh và một chiếc xe màu xanh"; Đặt kết quả = text.replace (/blue/g, "màu đỏ");

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương thức

"xxx".replace("", "_"); // "_xxx"
4 tìm kiếm một chuỗi cho một giá trị hoặc biểu thức chính quy.


Phương thức "xxx".replace("", "_"); // "_xxx" 4 trả về một chuỗi mới với (các) giá trị được thay thế.

Phương thức

"xxx".replace("", "_"); // "_xxx"
4 không thay đổi chuỗi ban đầu.

Ghi chú

  • Nếu bạn thay thế một giá trị, chỉ có trường hợp đầu tiên sẽ được thay thế. Để thay thế tất cả các trường hợp, hãy sử dụng biểu thức thông thường với bộ sửa đổi G.
  • Đọc thêm về các biểu thức thông thường trong của chúng tôi:


Hướng dẫn Regexp

Tham khảo RegEXP

Cú pháp

String.replace (SearchValue, NewValue)Thông số
Tham sốSự mô tả
The value, or regular expression, to search for.
SearchValueYêu cầu. Giá trị, hoặc biểu thức chính quy, để tìm kiếm.
The new value (to replace with).

NewValue

Bắt buộc. Giá trị mới (để thay thế bằng).Thông số
Tham sốSự mô tả


SearchValue

Yêu cầu. Giá trị, hoặc biểu thức chính quy, để tìm kiếm.

NewValue
let result = text.replace(/blue/gi, "red");

Hãy tự mình thử »

Bắt buộc. Giá trị mới (để thay thế bằng).

Giá trị trả lại
let result = text.replace(/blue|house|car/gi, function (x) {
  return x.toUpperCase();
});

Hãy tự mình thử »


Loại hình

Một chuỗi

Một chuỗi mới trong đó (các) giá trị được chỉ định đã được thay thế.

Nhiều ví dụ hơnThay thế toàn cầu, không nhạy cảm trường hợp: hãy để văn bản = "Mr Blue có một ngôi nhà màu xanh và một chiếc xe màu xanh"; Đặt kết quả = text.replace (/blue/gi, "màu đỏ");Một hàm để trả về văn bản thay thế: hãy để văn bản = "Mr Blue có một ngôi nhà màu xanh và một chiếc xe màu xanh"; Đặt kết quả = text.replace (/blue | house | car/gi, function (x) {& nbsp; & nbsp; return x.touppercase ();});Hỗ trợ trình duyệt
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).
"xxx".replace("", "_"); // "_xxx"
4 là tính năng ECMAScript1 (ES1).


Phương thức

"xxx".replace("", "_"); // "_xxx"
4 trả về một chuỗi mới với một, một số hoặc tất cả các trận đấu của
"xxx".replace("", "_"); // "_xxx"
9 được thay thế bằng
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0.
"xxx".replace("", "_"); // "_xxx"
9 có thể là một chuỗi hoặc
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2 và
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0 có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu. Nếu
"xxx".replace("", "_"); // "_xxx"
9 là một chuỗi, chỉ có lần xuất hiện đầu tiên sẽ được thay thế. Chuỗi ban đầu không thay đổi.
"xxx".replace("", "_"); // "_xxx"
4
method returns a new string with one, some, or all matches of a
"xxx".replace("", "_"); // "_xxx"
9 replaced by a
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0. The
"xxx".replace("", "_"); // "_xxx"
9 can be a string or a
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2, and the
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0 can be a string or a function called for each match. If
"xxx".replace("", "_"); // "_xxx"
9 is a string, only the first occurrence will be replaced. The original string is left unchanged.

Thử nó

Cú pháp

replace(pattern, replacement)

Thông số

"xxx".replace("", "_"); // "_xxx"
9

Có thể là một chuỗi hoặc một đối tượng với phương thức

"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
6 - & nbsp; ví dụ điển hình là một biểu thức chính quy. Bất kỳ giá trị nào không có phương thức
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
6 sẽ bị ép buộc thành một chuỗi.

"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0

Có thể là một chuỗi hoặc một hàm.

  • Nếu đó là một chuỗi, nó sẽ thay thế chuỗi con phù hợp với
    "xxx".replace("", "_"); // "_xxx"
    
    9. Một số mẫu thay thế đặc biệt được hỗ trợ; Xem chỉ định một chuỗi là phần thay thế bên dưới.
  • Nếu đó là một hàm, nó sẽ được gọi cho mỗi trận đấu và giá trị trả về của nó được sử dụng làm văn bản thay thế. Các đối số được cung cấp cho hàm này được mô tả trong chỉ định một hàm là phần thay thế bên dưới.

Giá trị trả về

Một chuỗi mới, với một, một số hoặc tất cả các trận đấu của mẫu được thay thế bằng cách thay thế được chỉ định.

Sự mô tả

Phương thức này không làm thay đổi giá trị chuỗi mà nó được gọi. Nó trả về một chuỗi mới.

Một mẫu chuỗi sẽ chỉ được thay thế một lần. Để thực hiện tìm kiếm toàn cầu và thay thế, hãy sử dụng biểu thức thông thường bằng cờ

function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}
0 hoặc sử dụng
function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}
1 thay thế.

Nếu

"xxx".replace("", "_"); // "_xxx"
9 là một đối tượng có phương thức
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
6 (bao gồm các đối tượng
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2), phương thức đó được gọi với chuỗi đích và
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
0 làm đối số. Giá trị trả về của nó trở thành giá trị trả về của
"xxx".replace("", "_"); // "_xxx"
4. Trong trường hợp này, hành vi của
"xxx".replace("", "_"); // "_xxx"
4 hoàn toàn được mã hóa bằng phương pháp
function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}
8 - ví dụ, bất kỳ đề cập nào về "các nhóm chụp" trong mô tả dưới đây thực sự là chức năng được cung cấp bởi
function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}
9.

Nếu

"xxx".replace("", "_"); // "_xxx"
9 là một chuỗi trống, sự thay thế được chuẩn bị để bắt đầu chuỗi.

"xxx".replace("", "_"); // "_xxx"

Một regexp với cờ

function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}
0 là trường hợp duy nhất mà
"xxx".replace("", "_"); // "_xxx"
4 thay thế nhiều lần. Để biết thêm thông tin về cách các thuộc tính Regex (đặc biệt là cờ dính) tương tác với
"xxx".replace("", "_"); // "_xxx"
4, xem
function replacer(match, p1, p2, p3, offset, string) {
  // p1 is non-digits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
const newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%
4.

Chỉ định một chuỗi là sự thay thế

Chuỗi thay thế có thể bao gồm các mẫu thay thế đặc biệt sau:

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is non-digits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
const newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%
5 và
function replacer(match, p1, p2, p3, offset, string) {
  // p1 is non-digits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
const newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%
6 chỉ khả dụng nếu đối số
"xxx".replace("", "_"); // "_xxx"
9 là đối tượng
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2. Nếu
"xxx".replace("", "_"); // "_xxx"
9 là một chuỗi hoặc nếu nhóm chụp tương ứng không có trong regex, thì mẫu sẽ được thay thế như một nghĩa đen. Nếu nhóm có mặt nhưng không được khớp (vì nó là một phần của sự phân tách), nó sẽ được thay thế bằng một chuỗi trống.

"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched

Chỉ định một chức năng là sự thay thế

Bạn có thể chỉ định một hàm là tham số thứ hai. Trong trường hợp này, chức năng sẽ được gọi sau khi trận đấu được thực hiện. Kết quả của hàm (giá trị trả về) sẽ được sử dụng làm chuỗi thay thế.

Lưu ý: Các mẫu thay thế đặc biệt được đề cập ở trên không áp dụng cho các chuỗi được trả về từ hàm thay thế. The above-mentioned special replacement patterns do not apply for strings returned from the replacer function.

Hàm có chữ ký sau:

function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
  return replacement;
}

Các đối số cho hàm như sau:

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
0

Các chuỗi con phù hợp. (Tương ứng với

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
1 ở trên.)

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
2

Chuỗi

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
3 được tìm thấy bởi một nhóm bắt giữ (bao gồm cả các nhóm chụp được đặt tên), cung cấp đối số đầu tiên cho
"xxx".replace("", "_"); // "_xxx"
4 là đối tượng
"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2. . Nếu nhóm là một phần của sự khác biệt (ví dụ:
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
4), thì sự thay thế chưa từng có sẽ là
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
5.

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
6

Phần bù của chuỗi con phù hợp trong toàn bộ chuỗi đang được kiểm tra. Ví dụ: nếu toàn bộ chuỗi là

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
7 và chuỗi con phù hợp là
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
8, thì đối số này sẽ là
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
9.

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
0

Toàn bộ chuỗi đang được kiểm tra.

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
1

Một đối tượng có các khóa là tên nhóm được sử dụng và có giá trị là các phần phù hợp (

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
5 nếu không khớp). Chỉ có mặt nếu
"xxx".replace("", "_"); // "_xxx"
9 chứa ít nhất một nhóm bắt giữ.

Số lượng chính xác các đối số phụ thuộc vào việc đối số đầu tiên có phải là đối tượng

"foo".replace(/(f)/, "$2"); // "$2oo"; the regex doesn't have the second group
"foo".replace("f", "$1"); // "$1oo"; the pattern is a string, so it doesn't have any groups
"foo".replace(/(f)|(g)/, "$2"); // "oo"; the second group exists but isn't matched
2 hay không - và, nếu vậy, có bao nhiêu nhóm chụp.

Ví dụ sau sẽ đặt

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
5 thành
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
6:

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is non-digits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
const newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%

Hàm sẽ được gọi nhiều lần cho mỗi trận đấu đầy đủ sẽ được thay thế nếu biểu thức chính quy trong tham số đầu tiên là toàn cầu.

Ví dụ

Xác định biểu thức chính quy trong thay thế ()

Trong ví dụ sau, biểu thức chính quy được xác định trong

"xxx".replace("", "_"); // "_xxx"
4 và bao gồm cờ trường hợp bỏ qua.

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...

Nhật ký này

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
8.

Sử dụng các cờ toàn cầu và Ignorecase với thay thế ()

Thay thế toàn cầu chỉ có thể được thực hiện với một biểu thức thông thường. Trong ví dụ sau, biểu thức chính quy bao gồm các cờ trường hợp toàn cầu và bỏ qua cho phép

"xxx".replace("", "_"); // "_xxx"
4 thay thế mỗi lần xuất hiện
function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
0 trong chuỗi bằng
function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
1.

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.

Điều này ghi lại

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
2.

Chuyển từ trong chuỗi

Tập lệnh sau đây chuyển các từ trong chuỗi. Đối với văn bản thay thế, tập lệnh sử dụng các nhóm chụp và các mẫu thay thế

const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
6 và
const str = 'Twas the night before Xmas...';
const newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
7.

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John

Điều này ghi lại

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
5.

Sử dụng hàm nội tuyến sửa đổi các ký tự phù hợp

Trong ví dụ này, tất cả các lần xuất hiện của các chữ cái viết hoa trong chuỗi được chuyển đổi thành chữ thường và dấu gạch nối được chèn ngay trước vị trí khớp. Điều quan trọng ở đây là các hoạt động bổ sung là cần thiết trên mục phù hợp trước khi nó được trả lại như một sự thay thế.

Hàm thay thế chấp nhận đoạn trích phù hợp làm tham số của nó và sử dụng nó để biến đổi trường hợp và kết hợp dấu gạch nối trước khi quay lại.

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}

Cho

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
6, điều này trả về
function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
7.

Bởi vì chúng tôi muốn chuyển đổi thêm kết quả của trận đấu trước khi thay thế cuối cùng, chúng tôi phải sử dụng một chức năng. Điều này buộc việc đánh giá trận đấu trước phương pháp

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
8. Nếu chúng tôi đã cố gắng làm điều này bằng cách sử dụng trận đấu mà không có chức năng,
function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
8 sẽ không có tác dụng.

const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work

Điều này là do

const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
0 trước tiên sẽ được đánh giá là một chuỗi theo nghĩa đen (dẫn đến cùng một
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
1) trước khi sử dụng các ký tự làm mẫu.

Thay thế một mức độ Fahrenheit bằng C, tương đương

Ví dụ sau đây thay thế một mức độ Fahrenheit với mức độ tương đương của nó. Bằng cấp Fahrenheit nên là một số kết thúc với

const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
2. Hàm trả về số Celsius kết thúc bằng
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
3. Ví dụ: nếu số đầu vào là
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
4, hàm trả về
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
5. Nếu số là
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
6, hàm trả về
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
7.

Biểu thức chính quy

const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
8 kiểm tra bất kỳ số nào kết thúc bằng
const newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
9. Số lượng độ Fahrenheit có thể truy cập được vào hàm thông qua tham số thứ hai của nó,
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
0. Hàm đặt số Celsius dựa trên số độ Fahrenheit được truyền trong một chuỗi cho hàm
"xxx".replace("", "_"); // "_xxx"
01.
"xxx".replace("", "_"); // "_xxx"
01 sau đó trả về số Celsius. Hàm này xấp xỉ cờ
"xxx".replace("", "_"); // "_xxx"
03 của Perl.

"xxx".replace("", "_"); // "_xxx"
0

Tạo ra một người thay thế chung

Giả sử chúng tôi muốn tạo một bộ thay thế nối dữ liệu bù vào mỗi chuỗi phù hợp. Bởi vì hàm thay thế đã nhận được tham số

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
6, nên nó sẽ là tầm thường nếu regex được biết đến một cách thống kê.

"xxx".replace("", "_"); // "_xxx"
1

Tuy nhiên, người thay thế này sẽ khó khái quát nếu chúng ta muốn nó hoạt động với bất kỳ mẫu Regex nào. Bộ thay thế là variadic - số lượng đối số mà nó nhận được phụ thuộc vào số lượng nhóm bắt giữ có mặt. Chúng ta có thể sử dụng các tham số REST, nhưng nó cũng sẽ thu thập

const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
6,
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
0, v.v. vào mảng. Thực tế là
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
1 có thể hoặc không được thông qua tùy thuộc vào danh tính của Regex cũng sẽ khiến cho việc biết đối số nào tương ứng với
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
6.

"xxx".replace("", "_"); // "_xxx"
2

Ví dụ

"xxx".replace("", "_"); // "_xxx"
09 ở trên không hoạt động khi Regex chứa một nhóm được đặt tên, bởi vì trong trường hợp này
"xxx".replace("", "_"); // "_xxx"
10 sẽ là
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
0 thay vì
const re = /apples/gi;
const str = 'Apples are round, and apples are juicy.';
const newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.
6.

Thay vào đó, bạn cần trích xuất một vài đối số cuối cùng dựa trên loại, vì

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
1 là một đối tượng trong khi
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
0 là một chuỗi.

"xxx".replace("", "_"); // "_xxx"
3

Thông số kỹ thuật

Sự chỉ rõ
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-string.prototype.replace
# sec-string.prototype.replace

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào để bạn thay thế một ký tự nhất định trong một chuỗi trong JavaScript?

Trả lời: Sử dụng phương thức thay thế javascript () Bạn có thể sử dụng phương thức JavaScript forth () để thay thế sự xuất hiện của bất kỳ ký tự nào trong chuỗi. Tuy nhiên, thay thế () sẽ chỉ thay thế lần xuất hiện đầu tiên của ký tự được chỉ định. Để thay thế tất cả sự xuất hiện, bạn có thể sử dụng công cụ sửa đổi toàn cầu (G).Use the JavaScript replace() method You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.

Làm cách nào để thay thế một ký tự trong một chuỗi?

Chuỗi Java Thay thế (char cũ, char mới) Ví dụ phương thức..
Lớp công khai thay thếexample1 {.
công khai void void chính (chuỗi args []) {.
Chuỗi S1 = "Javatpoint là một trang web rất tốt" ;.
Chuỗi thay thế = s1.Replace ('a', 'e'); // thay thế tất cả các lần xuất hiện của 'a' thành 'e'.
System.out.println(replaceString);.

Thay thế () trong javascript là gì?

Phương thức thay thế () tìm kiếm một chuỗi cho một giá trị hoặc biểu thức chính quy.Phương thức thay thế () trả về một chuỗi mới với (các) giá trị được thay thế.Phương thức thay thế () không thay đổi chuỗi gốc.searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

Bạn có thể sửa đổi chuỗi trong JavaScript không?

Các chuỗi JavaScript là bất biến, chúng không thể được sửa đổi "tại chỗ" để bạn không thể sửa đổi một ký tự duy nhất.Trong thực tế, mọi sự xuất hiện của cùng một chuỗi là một đối tượng.they cannot be modified "in place" so you cannot modify a single character. in fact every occurence of the same string is ONE object.