当前位置:网站首页>2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)

2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)

2022-06-24 22:38:00 Little radish

One 、 Determine the number of digits in a string number

 Insert picture description here
give an example :

console.log('10'.length); // 2
console.log('120'.length); // 3

Two 、 Judge the number of digits of numerical type

 Insert picture description here

Need to go through first Numeric number +'' take Numeric number convert to String number .

give an example :

console.log((10+'').length); // 2
console.log((120+'').length); // 3

3、 ... and 、 Limit text length ( At most n A word , beyond ...

/* text:  Text content  n:  Limit length  */
function limitTextLength(text,n) {
    
	return text.match(/[\u4E00-\[email protected]#$%^&*()_+-=.]/g).length>n?text.substring(0,n)+'…':text
}

limitTextLength(' It will be a great task for us ',6); // ' The great task will fall on …'
limitTextLength('Rabbits like carrots',9); // 'Rabbits l…'
limitTextLength(' Pink &&Rabbits like carrots',9); // ' Pink &&Rabbi…'

【 Add 】 Limit the format of text content according to project requirements , Stay if necessary 、 If not, delete .

\u4E00-\[email protected]#$%^&*()_+-=. Split into four parts from left to right :

  • \u4E00-\u9FA5 Chinese characters
  • a-zA-Z Case letters
  • 0-9 Numbers
  • [email protected]#$%^&*()_+-=. Special characters

 Insert picture description here

原网站

版权声明
本文为[Little radish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241632081909.html