Có thể thay thế một char bằng một con sên?
Ngày đăng:
18/12/2022
Trả lời:
0
Lượt xem:
76
Bắt đầu nội dung chính
OSCC · JavaScript, String, Regexp · 4 tháng 10, 2020 Chuyển đổi một chuỗi thành một con sên thân thiện với URL const slugify = str => str .toLowerCase() .trim() .replace(/[^\w\s-]/g, '') .replace(/[\s_-]+/g, '-') .replace(/^-+|-+$/g, ''); Đoạn trích được đề xuất
Có lẽ có rất nhiều công thức 2 bị xiềng xích. Điều đó hoặc lấy chuỗi của bạn làm đầu vào và diễn giải lại nó bằng một công thức như Tệp 3 chính chứa tất cả các ký tự đã biết và phiên âm của chúng. Tất cả các ký tự mới nên được thêm vào đó trước. Trong trường hợp bạn tình cờ gặp một ký tự đã được đặt trong 3, nhưng không được phiên âm chính xác theo ngôn ngữ của bạn, thì bạn phải thêm các ký tự đó vào 5 để ghi đè lên phiên âm đã có trong 3, nhưng chỉ dành cho ngôn ngữ của bạnBạn có thể lấy mã ngôn ngữ chính xác của ngôn ngữ của mình từ đây Gia hạnNgoài hộp, slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })1 có hỗ trợ một số ký hiệu Unicode. Ví dụ: ký hiệu slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })2 (phóng xạ) không được xác định trong slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })3 và do đó, nó sẽ bị loại bỏ theo mặc định slugify('unicode ♥ is ☢') // unicode-love-is Tuy nhiên, bạn có thể mở rộng các ký hiệu được hỗ trợ hoặc ghi đè các ký hiệu hiện có bằng biểu tượng của riêng bạn slugify.extend({'☢': 'radioactive'}) slugify('unicode ♥ is ☢') // unicode-love-is-radioactive Hãy nhớ rằng phương pháp slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })4 mở rộng/ghi đè lên mặc định slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })3 cho toàn bộ quá trình. Trong trường hợp bạn cần một phiên bản mới của đối tượng slugify('some string', { replacement: '-', // replace spaces with replacement character, defaults to `-` remove: undefined, // remove characters that match regex, defaults to `undefined` lower: false, // convert to lower case, defaults to `false` strict: false, // strip special characters except replacement, defaults to `false` locale: 'vi', // language code of the locale to use trim: true // trim leading and trailing replacement chars, defaults to `true` })3 của slugify, trước tiên bạn phải dọn sạch bộ đệm của mô-đun |