Home > Web Front-end > JS Tutorial > body text

Dynamically obtain the number of bytes and characters of the current input content

php中世界最好的语言
Release: 2018-04-16 11:49:50
Original
1901 people have browsed it

This time I will bring you the method of dynamically obtaining the number of bytes and characters of the current input content, and dynamically obtaining the number of bytes and characters of the current input content. What are the precautions?. The following is a practical case. Get up and take a look.

Detailed example of dynamically counting the number of bytes and characters of the current input content

I saw that there are many methods using charAt on the Internet. The principle is also very simple. Use regular expressions to determine whether it is Chinese. If so, add 2 to the number of bytes. If not, add 1 to the number of bytes.

Later I found another way. Since one Chinese character corresponds to two bytes, you can use regular rules to replace the matched Chinese character with two characters, for example: "xx", and finally get the converted string## The length of # is the number of bytes.

The number of characters corresponds to how many Chinese characters, letters, and symbols there are.

//短信内容字数
  $scope.bytesCount= 0;
  $scope.smsLength = 0;
  $scope.smsContent = '1';
  $scope.smsCount = function () {
    $scope.bytesCount = $scope.smsContent.replace(/[^\x00-\xff]/g, 'xx').length;
    $scope.smsLength = $scope.smsContent.length;
  }
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Double linked list definition of JS data structure

How to use multer upload

The above is the detailed content of Dynamically obtain the number of bytes and characters of the current input content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!