RegExp.lastParen ($+)

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: Tất cả các thuộc tính tĩnh của RegExp hiển thị trạng thái khớp cuối cùng trên toàn cục đều đã bị deprecated. Xem các tính năng RegExp deprecated để biết thêm thông tin.

Thuộc tính accessor tĩnh RegExp.lastParen trả về chuỗi con khớp trong ngoặc đơn cuối cùng, nếu có. RegExp["$+"] là bí danh của thuộc tính này.

Mô tả

lastParen là thuộc tính tĩnh của RegExp, bạn luôn dùng nó như RegExp.lastParen hoặc RegExp["$+"], thay vì như thuộc tính của một đối tượng RegExp bạn đã tạo.

Giá trị của lastParen cập nhật mỗi khi một instance RegExp (nhưng không phải subclass của RegExp) tạo ra một khớp thành công. Nếu chưa có khớp nào được thực hiện, hoặc nếu lần thực thi regex gần nhất không có capturing group nào, lastParen là một chuỗi rỗng. Set accessor của lastParenundefined, vì vậy bạn không thể thay đổi thuộc tính này trực tiếp.

Bạn không thể dùng bí danh viết tắt với dot property accessor (RegExp.$+), vì + không phải là một phần hợp lệ của identifier, nên điều này gây ra SyntaxError. Thay vào đó, hãy dùng bracket notation.

Ví dụ

Sử dụng lastParen và $+

js
const re = /(hi)/g;
re.test("hi there!");
RegExp.lastParen; // "hi"
RegExp["$+"]; // "hi"

Thông số kỹ thuật

Specification
Legacy RegExp features
# additional-properties-of-the-regexp-constructor

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

Xem thêm