search
HomeWeb Front-endJS TutorialHow to determine whether text is full-width or half-width in JS

Full-width: It is a kind of computer character, which means that one full-width character occupies the position of two standard characters (or two half-width characters). Full width occupies two bytes. Half-width: refers to a character occupying a standard character position. Half-width occupies one byte. Next, this article will introduce you to the knowledge of JS verification of full-width and half-width and mutual conversion. Friends who need it can refer to it

1. What are full-width and half-width?

Full-width: It is a kind of computer character, which means that one full-width character occupies the position of two standard characters (or two half-width characters). Full width occupies two bytes.

Chinese characters, English characters that specify full-width, and graphic symbols in the national standard GB2312-80 and special characters are all full-width characters. In full-width, letters and numbers occupy equal-width positions like Chinese characters.

Half-width: refers to a character occupying a standard character position. Half-width occupies one byte.

Half-width characters are ASCII characters. When no Chinese character input method works, the letters, numbers and characters entered are all half-width characters.

Each half-width character only occupies one byte of space (one byte has 8 bits, a total of 256 encoding spaces). The font size of hieroglyphic languages ​​such as Chinese, Japanese, and Korean is much larger than 256 encoding spaces, so two bytes are used for storage. At the same time, due to the writing habits of hieroglyphics such as China, Japan, and Korea, if full-width characters are used uniformly, the arrangement will appear neat.

In order to arrange neatly, English and other Latin characters and punctuation are also provided in full-width format.

2. The difference between full-width and half-width

Full-width and half-width are mainly for punctuation marks. Full-width punctuation occupies two bytes. , half-width occupies one byte. Regardless of whether it is half-width or full-width, Chinese characters take up two bytes.

3.js determine whether the entered text is full-width or half-width?

str="中文;;a"   
alert(str.match(/[\u0000-\u00ff]/g))   //半角  
alert(str.match(/[\u4e00-\u9fa5]/g))   //中文  
alert(str.match(/[\uff00-\uffff]/g))   //全角

4.js mutual conversion of full-width and half-width

First of all, the following information must be clarified:

a. A full-width space is 12288, and a half-width space is 32

b. The corresponding relationship between half-width (33-126) and full-width (65281-65374) for other characters is: the difference is 65248

Convert half-width to full-width

function ToDBC(txtstring) { 
  var tmp = ""; 
  for(var i=0;i<txtstring.length;i++{ 
    if(txtstring.charCodeAt(i)==32){ 
      tmp= tmp+ String.fromCharCode(12288); 
    } 
    if(txtstring.charCodeAt(i)<127){ 
      tmp=tmp+String.fromCharCode(txtstring.charCodeAt(i)+65248); 
    } 
  } 
  return tmp; 
}

The charCodeAt() method and fromCharCode() method of js are used above.

The charCodeAt() method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0 - 65535.

fromCharCode() accepts a specified Unicode value and returns a string.

If you want to know more about the charCodeAt() method and fromCharCode() method, you can click "JavaScript charCodeAt() Method" and "JavaScript fromCharCode() Method".

Convert full-width to half-width

function ToCDB(str) { 
  var tmp = ""; 
  for(var i=0;i<str.length;i++){ 
    if (str.charCodeAt(i) == 12288){
      tmp += String.fromCharCode(str.charCodeAt(i)-12256);
      continue;
    }
    if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375){ 
      tmp += String.fromCharCode(str.charCodeAt(i)-65248); 
    } 
    else{ 
      tmp += String.fromCharCode(str.charCodeAt(i)); 
    } 
  } 
  return tmp 
}

[Related recommendations]

1. Javascript Free Video Tutorial

2. nodejs+websocket completes a chat system function

3. Js completes the countdown time difference effect

4. Detailed explanation of the method of recursively deleting elements in an array in JS

##5.

Detailed explanation of an example of JS completing the star rating function

The above is the detailed content of How to determine whether text is full-width or half-width in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version