sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

    • 序言 👏
    • escapeHTML【转义HTML】
    • stripHTMLTags【删除字符串HTMl标签】
    • lowerCamelCase【小驼峰命名,首字符小写】
    • upperCamelCase【大驼峰命名】
    • capitalize【首字母大写】
    • camelCase【小驼峰命名】
    • kebabCase【中横线命名】
    • snakeCase【蛇形命名法】
      • 1.示例
      • 2.入参说明
      • 3.源码
    • isSnakeCase【是否是蛇形命名】
  • 数学-Math

  • 文件-Buffer

  • 节点-dom

  • 拓展

  • nodejs

目录

snakeCase【蛇形命名法】

描述

将字符串转成蛇形命名法

# 1.示例

import { snakeCase } from 'sf-utils2'

console.log(snakeCase('string')) // => string
console.log(snakeCase('camelCase')) // => camel_case
console.log(snakeCase('param-case')) // => param_case
console.log(snakeCase('PascalCase')) // => pascal_case
console.log(snakeCase('UPPER_CASE')) // => upper_case
console.log(snakeCase('snake_case')) // => snake_case
console.log(snakeCase('sentence case')) // => sentence_case
console.log(snakeCase('Title Case')) // => title_case
console.log(snakeCase('dot.case')) // => // dot_case

console.log(snakeCase('')) // => ''
console.log(snakeCase(null)) // => ''
console.log(snakeCase(undefined)) // => ''

console.log(snakeCase('Abc ___ 123 ___ xYz')) // => abc_123_x_yz
console.log(snakeCase('123__abc  ... ?// {#} def 12')) // => 123_abc_def_12
console.log(snakeCase('	tab space ??? ________')) // => tab_space
console.log(snakeCase('___?||123  abc|| 123..123')) // => 123_abc_123_123
console.log(snakeCase('!@#$%  {}|":;" ABC XyZ G123H')) // => abc_xy_z_g123h
console.log(snakeCase(' ^&* #DEFine x: 15 + ==')) // => define_x_15
console.log(snakeCase('123456789')) // => 123456789
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 2.入参说明

参数 说明 类型 是否必填 默认值
str 要调整的字符串 String - -

# 3.源码

源码,点开查看 👈
/**
 * 将字符串转成蛇形命名法
 * @param {String} str
 * @returns {string}
 */
function snakeCase(str) {
  if (!str) return ''
  return String(str)
    .replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g, '')
    .replace(/([a-z])([A-Z])/g, (m, a, b) => a + '_' + b.toLowerCase())
    .replace(/[^A-Za-z0-9]+|_+/g, '_')
    .toLowerCase()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2023/06/24, 19:35:48
kebabCase【中横线命名】
isSnakeCase【是否是蛇形命名】

← kebabCase【中横线命名】 isSnakeCase【是否是蛇形命名】→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×