Home  >  Article  >  Web Front-end  >  JavaScript determines whether it is a symbol

JavaScript determines whether it is a symbol

WBOY
WBOYOriginal
2023-05-16 10:44:071166browse

In Javascript, you can use regular expressions or character encoding to determine whether a character is a symbol. A symbol refers to any character that is not a letter or number. This article will introduce several common methods to determine whether a character is a symbol.

  1. Using regular expressions

In Javascript, regular expressions can be used to quickly and easily determine whether a character is a symbol. The following is a regular expression used to determine whether a character is a symbol:

const isSymbol = /[^ws]/;

In the regular expression, w represents a letter or number, and s represents a space character. So 1 means any character that is not an alphabetic or numeric or space character. Use the test() method to determine whether a character matches the regular expression.

Sample code:

const isSymbol = /[^ws]/;
console.log(isSymbol.test("?")); // true,?是符号
console.log(isSymbol.test("a")); // false,a是字母
console.log(isSymbol.test("1")); // false,1是数字
console.log(isSymbol.test(" ")); // false,空格不是符号
  1. Using character encoding

In the computer, each character has a corresponding character encoding. In ASCII encoding, the encoding range of symbols is 32-47, 58-64, 91-96, 123-126. We can use character encoding to determine whether a character is a symbol.

Sample code:

function isSymbol(char) {
  const code = char.charCodeAt(0);
  if ((code >= 32 && code <= 47) || (code >= 58 && code <= 64) ||
      (code >= 91 && code <= 96) || (code >= 123 && code <= 126)) {
    return true;
  }
  return false;
}

console.log(isSymbol("?")); // true,?是符号
console.log(isSymbol("a")); // false,a是字母
console.log(isSymbol("1")); // false,1是数字
console.log(isSymbol(" ")); // false,空格不是符号
  1. Using Unicode encoding

Unicode is a character encoding standard that includes characters and symbols in almost all languages. We can use Unicode encoding to determine whether a character is a symbol.

Sample code:

function isSymbol(char) {
  const unicode = char.charCodeAt(0);
  if ((unicode >= 33 && unicode <= 47) || (unicode >= 58 && unicode <= 64) ||
      (unicode >= 91 && unicode <= 96) || (unicode >= 123 && unicode <= 126)) {
    return true;
  }
  return false;
}

console.log(isSymbol("?")); // true,?是符号
console.log(isSymbol("a")); // false,a是字母
console.log(isSymbol("1")); // false,1是数字
console.log(isSymbol(" ")); // false,空格不是符号

Summary

This article introduces three common methods of determining whether a character is a symbol, namely using regular expressions, character encoding and Unicode encoding. Using regular expressions is the easiest method, but there may be a performance loss; using character encoding and Unicode encoding is more cumbersome, but has higher performance. Readers can choose a method that suits them based on specific scenarios to determine whether a character is a symbol.


  1. ws

The above is the detailed content of JavaScript determines whether it is a symbol. 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