RegExp.prototype.compile()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Note: Phương thức compile() chỉ được quy định vì lý do tương thích. Việc sử dụng compile() khiến source và flags của regex vốn là bất biến trở nên có thể thay đổi, điều này có thể làm người dùng bất ngờ. Thay vào đó, bạn có thể dùng constructor RegExp() để tạo một đối tượng regular expression mới.

Phương thức compile() của các instance RegExp được dùng để biên dịch lại một regular expression với source và flags mới sau khi đối tượng RegExp đã được tạo.

Cú pháp

js
compile(pattern, flags)

Tham số

pattern

Văn bản của regular expression.

flags

Bất kỳ tổ hợp nào của các giá trị flag.

Giá trị trả về

Không có (undefined).

Ngoại lệ

TypeError

Được ném ra nếu giá trị this không phải là instance của constructor RegExp trong realm hiện tại. Bao gồm cả subclass của RegExp và constructor RegExp từ realm khác.

Ví dụ

Sử dụng compile()

Ví dụ sau đây minh họa cách biên dịch lại một regular expression với pattern và flag mới.

js
const regexObj = /foo/gi;
regexObj.compile("new foo", "g");

Thông số kỹ thuật

Specification
ECMAScript® 2027 Language Specification
# sec-regexp.prototype.compile

Tương thích trình duyệt

Xem thêm