在JavaScript中使用正则表达式。正则表达式是用于描述搜索模式的特殊文本字符串,它以/ pattern / modifiers的形式编写,其中“ pattern”是正则表达式本身,“ modifiers”是一系列表示各种选项的字符。
在文字匹配之后
,字符类是最基本的正则表达式概念。它使一个小的字符序列与更大的一组字符匹配。例如,[A-Z]
可以代表大写字母,并且\d
可以表示任何数字。
从下面的例子
contains_alphaNumeric
«它检查字符串是否包含字母或数字(或同时包含字母和数字)。该连字符( - )将被忽略。
onlyMixOfAlphaNumeric
«它检查字符串是否仅包含任何序列顺序的字母和数字。
例:
function matchExpression( str ) {
var rgularExp = {
contains_alphaNumeric : /^(?!-)(?!.*-)[A-Za-z0-9-]+(?<!-)$/,
containsNumber : /\d+/,
containsAlphabet : /[a-zA-Z]/,
onlyLetters : /^[A-Za-z]+$/,
onlyNumbers : /^[0-9]+$/,
onlyMixOfAlphaNumeric : /^([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*$/
}
var expMatch = {};
expMatch.containsNumber = rgularExp.containsNumber.test(str);
expMatch.containsAlphabet = rgularExp.containsAlphabet.test(str);
expMatch.alphaNumeric = rgularExp.contains_alphaNumeric.test(str);
expMatch.onlyNumbers = rgularExp.onlyNumbers.test(str);
expMatch.onlyLetters = rgularExp.onlyLetters.test(str);
expMatch.mixOfAlphaNumeric = rgularExp.onlyMixOfAlphaNumeric.test(str);
return expMatch;
}
// HTML Element attribute's[id, name] with dynamic values.
var id1 = "Yash", id2="777", id3= "Yash777", id4= "Yash777Image4"
id11= "image5.64", id22= "55-5.6", id33= "image_Yash", id44= "image-Yash"
id12= "_-.";
console.log( "Only Letters:\n ", matchExpression(id1) );
console.log( "Only Numbers:\n ", matchExpression(id2) );
console.log( "Only Mix of Letters and Numbers:\n ", matchExpression(id3) );
console.log( "Only Mix of Letters and Numbers:\n ", matchExpression(id4) );
console.log( "Mixed with Special symbols" );
console.log( "Letters and Numbers :\n ", matchExpression(id11) );
console.log( "Numbers [-]:\n ", matchExpression(id22) );
console.log( "Letters :\n ", matchExpression(id33) );
console.log( "Letters [-]:\n ", matchExpression(id44) );
console.log( "Only Special symbols :\n ", matchExpression(id12) );
输出:
Only Letters:
{containsNumber: false, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: true, mixOfAlphaNumeric: false}
Only Numbers:
{containsNumber: true, containsAlphabet: false, alphaNumeric: true, onlyNumbers: true, onlyLetters: false, mixOfAlphaNumeric: false}
Only Mix of Letters and Numbers:
{containsNumber: true, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: true}
Only Mix of Letters and Numbers:
{containsNumber: true, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: true}
Mixed with Special symbols
Letters and Numbers :
{containsNumber: true, containsAlphabet: true, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Numbers [-]:
{containsNumber: true, containsAlphabet: false, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Letters :
{containsNumber: false, containsAlphabet: true, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Letters [-]:
{containsNumber: false, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Only Special symbols :
{containsNumber: false, containsAlphabet: false, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
java模式与正则表达式匹配。