简体   繁体   中英

String pattern matching in Javascript

I am doing some self learning about Patern Matching in Javascript.
I got a simple input text field in a HTML web page,
and I have done some Javascript to capture the string and check if there
are any strange characters other than numbers and characters in the string.
But I am not sure if it is correct.
Only numbers, characters or a mixture of numbers and characters are allowed.

var pattern = /^[a-z]+|[A-Z]+|[0-9]+$/;

And I have another question about Pattern Matching in Javascript,
what does the percentage symbol mean in Pattern matching. For example:

var pattern = '/[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[AZ]{2,4}/';

You can put multiple character ranges inside a [] class.

var pattern = /^[a-zA-Z0-9]+$/
//or 
var pattern = /^[a-z0-9]+$/i // <- using "case insensitive" modifier

Percentage symbol means percentage symbol will be matched, it has no special meaning inside a regular expression.

JavaScript正则表达式中的%没有任何特殊含义0-它只是一个匹配的字符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM