Home > Article > Web Front-end > 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
Breaking down the pattern:
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 _.-]+)$/
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!