Có thể thay thế một char bằng một con sên?

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

  • Chuyển đổi một chuỗi màu

    thisRow.Name.Split[''].FormulaMap[
      Switch[CurrentValue,
        'ą', 'a',
        'ć', 'c',
        'ł', 'l',
       .. the rest of your replacements ...
        CurrentValue
      ]
    ].Concatenate[].Lower[].RegexReplace[" ", "-"]
    
    0 thành một đối tượng với các giá trị của mỗi màu

  • Chuyển đổi một chuỗi màu

    thisRow.Name.Split[''].FormulaMap[
      Switch[CurrentValue,
        'ą', 'a',
        'ć', 'c',
        'ł', 'l',
       .. the rest of your replacements ...
        CurrentValue
      ]
    ].Concatenate[].Lower[].RegexReplace[" ", "-"]
    
    1 thành một đối tượng với các giá trị của mỗi màu

  • Chuyển đổi một chuỗi thành trường hợp tiêu đề

Có lẽ có rất nhiều công thức

thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]
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ư

thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]

Tệp

thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]
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
thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]
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
thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]
5 để ghi đè lên phiên âm đã có trong
thisRow.Name.Split[''].FormulaMap[
  Switch[CurrentValue,
    'ą', 'a',
    'ć', 'c',
    'ł', 'l',
   .. the rest of your replacements ...
    CurrentValue
  ]
].Concatenate[].Lower[].RegexReplace[" ", "-"]
3, nhưng chỉ dành cho ngôn ngữ của bạn

Bạ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ạn

Ngoà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

Chủ Đề