Home > Web Front-end > JS Tutorial > How to Create an Alphanumeric-Only Regular Expression in JavaScript?

How to Create an Alphanumeric-Only Regular Expression in JavaScript?

DDD
Release: 2024-11-02 22:21:30
Original
1010 people have browsed it

How to Create an Alphanumeric-Only Regular Expression in JavaScript?

Alphanumeric-Only Regular Expression in JavaScript

When working with text input, it's often necessary to restrict characters to alphanumeric only (a-z, A-Z, 0-9). However, regular expressions that allow alphanumeric characters often require both letters and numbers.

For scenarios where either letters or numbers should be allowed individually, a different regular expression is needed. The following pattern matches alphanumeric characters without the need for both:

/^[a-z0-9]+$/i
Copy after login

Breaking down the pattern:

  • ^: Start of the string.
  • [a-z0-9]: Matches lowercase letters (a-z) or numbers (0-9).
  • : Indicates that the previous pattern must occur one or more times.
  • $: End of the string.
  • i: Case-insensitive matching.

Supporting Universal Characters

If you need to allow universal characters, such as characters from different languages, you can modify the regular expression to include those characters. For example, to support Persian characters, use the following pattern:

/^([a-zA-Z0-9\u0600-\u06FF\u0660-\u0669\u06F0-\u06F9 _.-]+)$/
Copy after login

Here, additional Unicode character ranges have been added to match Persian characters.

The above is the detailed content of How to Create an Alphanumeric-Only Regular Expression in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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