如何增强 slugify 来处理 Camel Case?

发布于 2024-12-28 15:15:04 字数 476 浏览 1 评论 0原文

我想编写一个 JavaScript 函数来 slugify string,还有一个要求:处理驼峰式大小写。例如,thisIsCamelCase 将变为 this-is-camel-case

如何修改这样的函数这样做吗?

编辑

添加了完整的已回答的示例

I'd like to write a JavaScript function to slugify a string, with one more requirement: handle Camel Case. For example, thisIsCamelCase would become this-is-camel-case.

How do I modify a function like this to do so?

EDIT

Added full, answered example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨死的猪 2025-01-04 15:15:04

您只需要在您发布的内容中添加一行代码

str = str.replace(/[A-Z]/g, function(s){ return "-" + s; });

即可编辑剩余的代码

function string_to_slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.replace(/[A-Z]/g, function(s){ return "-" + s; }); // camel case handling
  str = str.toLowerCase();

  // remove accents, swap ñ for n, etc
  var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;",
      to   = "aaaaeeeeiiiioooouuuunc------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(from[i], to[i]);
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

  return str;
}

::我也删除了无用的新 RegExp 逻辑。
编辑:: 添加“g”以查找所有大写字母,而不仅仅是第一个大写字母。

You just need to add one line of code to what you posted

str = str.replace(/[A-Z]/g, function(s){ return "-" + s; });

This brings the remaining code to be

function string_to_slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.replace(/[A-Z]/g, function(s){ return "-" + s; }); // camel case handling
  str = str.toLowerCase();

  // remove accents, swap ñ for n, etc
  var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;",
      to   = "aaaaeeeeiiiioooouuuunc------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(from[i], to[i]);
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

  return str;
}

Edit :: I also deleted the useless new RegExp logic too.
Edit :: Added 'g' to find all caps, not just the first one.

国粹 2025-01-04 15:15:04

这会对你有帮助!

     var strings = 'toDo';
var i=0;
var ch='';

while (i < strings.length){
    character = strings.charAt(i);
        if (character == character.toUpperCase()) {
            //alert ('upper case true' + character);
        character = "-" + character.toLowerCase();
        }
      i++;
      ch += character;
    }
//to-do
      alert(ch);

This will help you!

     var strings = 'toDo';
var i=0;
var ch='';

while (i < strings.length){
    character = strings.charAt(i);
        if (character == character.toUpperCase()) {
            //alert ('upper case true' + character);
        character = "-" + character.toLowerCase();
        }
      i++;
      ch += character;
    }
//to-do
      alert(ch);
青衫儰鉨ミ守葔 2025-01-04 15:15:04

尝试使用:

str=str.toLowerCase() before or after the slugification.

Try using:

str=str.toLowerCase() before or after the slugification.

我想用蛇键生成一个蛞蝓。这个方法对我有用

$(document).on('keyup', '.slugGen', function () {
   let slug = $(this).val().toLowerCase().replace(/ /g, '_').replace(/[^\w-]+/g, '');
   $(this).closest('tr').find('#slug').val(slug);
});

I wanted to generate a slug in snake keys. This method worked for me

$(document).on('keyup', '.slugGen', function () {
   let slug = $(this).val().toLowerCase().replace(/ /g, '_').replace(/[^\w-]+/g, '');
   $(this).closest('tr').find('#slug').val(slug);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文