当前位置:网站首页>2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
2022-06-24 19:35:00 【小呀小萝卜儿】
一、判断字符串型数字有几位

举例:
console.log('10'.length); // 2
console.log('120'.length); // 3
二、 判断数值型数字有几位

需要先通过
数值型数字+''将 数值型数字 转换成 字符串型数字。
举例:
console.log((10+'').length); // 2
console.log((120+'').length); // 3
三、限制文本长度(最多展示n个字,超出...)
/* text: 文本内容 n: 限制长度 */
function limitTextLength(text,n) {
return text.match(/[\u4E00-\[email protected]#$%^&*()_+-=.]/g).length>n?text.substring(0,n)+'…':text
}
limitTextLength('天将降大任于斯人也',6); // '天将降大任于…'
limitTextLength('Rabbits like carrots',9); // 'Rabbits l…'
limitTextLength('粉色&&Rabbits like carrots',9); // '粉色&&Rabbi…'
【补充】 根据项目需求限制文本内容的格式,要则留、不要则删。
\u4E00-\[email protected]#$%^&*()_+-=.从左往右拆分为四个部分:
\u4E00-\u9FA5(汉字)a-zA-Z(大小写字母)0-9(数字)[email protected]#$%^&*()_+-=.(特殊字符)

边栏推荐
- HTTP的缓存控制
- 软件设计的七大原则
- Principles of Ethernet port mirroring, link aggregation and VLAN Technology
- Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
- Docker installs MySQL 8.0. Detailed steps
- KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
- 60 divine vs Code plug-ins!!
- Future development of education industry of e-commerce Express
- 树莓派初步使用
- 故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
猜你喜欢

NiO zero copy

Future development of education industry of e-commerce Express

envoy获取客户端真实IP

Detailed explanation of agency mode

How to extract dates from web pages?

Data center basic network platform

DAO 中常见的投票治理方式

ThreadLocal内存泄漏问题

Idea close global search box
![leetcode:515. Find the maximum value in each tree row [brainless BFS]](/img/87/1926d783fb6f8d8439213d86b5da40.png)
leetcode:515. Find the maximum value in each tree row [brainless BFS]
随机推荐
How does flutter use the online transcoding tool to convert JSON to model
代理模式详解
Main steps of system test
EasyBypass
磁盘的结构
Embedded development: tips and tricks -- clean jump from boot loader to application code
AQS source code analysis
Flutter: Unsupported value: false/true
Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection
【个人实验报告】
Ideal L9, new trend of intelligent cockpit
Use of selector for NiO multiplexing
Servlet详解
堆內存分配的並發問題
问题求解——嵌套列表
Seven principles of software design
YGG recent game partners list
Cross border e-commerce, early entry and early benefit
Ansible basic configuration
重磅!法大大上榜“专精特新”企业