是否有正则表达式检查字符串是否只包含大写字母、小写字母、数字和下划线?


当前回答

要求的格式

允许以下三点:

0142171547295 014 - 2171547295 123年美国广播公司

不允许其他格式:

validatePnrAndTicketNumber(){
    let alphaNumericRegex=/^[a-zA-Z0-9]*$/;
    let numericRegex=/^[0-9]*$/;
    let numericdashRegex=/^(([1-9]{3})\-?([0-9]{10}))$/;
   this.currBookingRefValue = this.requestForm.controls["bookingReference"].value;
   if(this.currBookingRefValue.length == 14 && this.currBookingRefValue.match(numericdashRegex)){
     this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else if(this.currBookingRefValue.length ==6 && this.currBookingRefValue.match(alphaNumericRegex)){
    this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else if(this.currBookingRefValue.length ==13 && this.currBookingRefValue.match(numericRegex) ){
    this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else{
    this.requestForm.controls["bookingReference"].setErrors({'pattern': true});
   }
}
<input name="booking_reference" type="text" [class.input-not-empty]="bookingRef.value"
    class="glyph-input form-control floating-label-input" id="bookings_bookingReference"
    value="" maxlength="14" aria-required="true" role="textbox" #bookingRef
    formControlName="bookingReference" (focus)="resetMessageField()" (blur)="validatePnrAndTicketNumber()"/>

其他回答

要求的格式

允许以下三点:

0142171547295 014 - 2171547295 123年美国广播公司

不允许其他格式:

validatePnrAndTicketNumber(){
    let alphaNumericRegex=/^[a-zA-Z0-9]*$/;
    let numericRegex=/^[0-9]*$/;
    let numericdashRegex=/^(([1-9]{3})\-?([0-9]{10}))$/;
   this.currBookingRefValue = this.requestForm.controls["bookingReference"].value;
   if(this.currBookingRefValue.length == 14 && this.currBookingRefValue.match(numericdashRegex)){
     this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else if(this.currBookingRefValue.length ==6 && this.currBookingRefValue.match(alphaNumericRegex)){
    this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else if(this.currBookingRefValue.length ==13 && this.currBookingRefValue.match(numericRegex) ){
    this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
   }else{
    this.requestForm.controls["bookingReference"].setErrors({'pattern': true});
   }
}
<input name="booking_reference" type="text" [class.input-not-empty]="bookingRef.value"
    class="glyph-input form-control floating-label-input" id="bookings_bookingReference"
    value="" maxlength="14" aria-required="true" role="textbox" #bookingRef
    formControlName="bookingReference" (focus)="resetMessageField()" (blur)="validatePnrAndTicketNumber()"/>

对于那些寻找unicode字母数字匹配的人,你可能想做一些类似的事情:

^[\p{L} \p{Nd}_]+$

进一步的阅读请参阅Unicode正则表达式(Unicode Consortium)和Unicode正则表达式(Regular-Expressions.info)。

^\w*$将适用于以下组合:

1
123
1av
pRo
av1

这对我很有用。我在O'Reilly的“精通正则表达式”中发现了这个:

/^\w+$/

解释:

^声明字符串开始的位置 \w+匹配任何单词字符(等于[a-zA-Z0-9_]) “+”量词-在一次和无限次之间进行匹配,尽可能多地进行匹配,根据需要进行回馈(贪婪) $在字符串的末尾断言位置

验证自己:

Const regex = /^\w+$/ Const STR = ' nut_cracker_12 '; 让米; If ((m = regex.exec(str)) !== null) { //结果可以通过' m '变量访问。 m.forEach((match, groupIndex) => { console.log('找到匹配,组${groupIndex}: ${match} '); }); }

试试我为字符串做的这些多语言扩展。

isalphannumeric—字符串必须包含至少一个alpha(在Unicode范围内的字母,在charSet中指定)和至少一个数字(在numSet中指定)。同样,字符串应该只包含alpha和数字。

IsAlpha -字符串应该包含至少一个alpha(在指定的语言字符集中),并且只由alpha组成。

IsNumeric -字符串应该至少包含一个数字(使用指定的语言numSet),并且仅由数字组成。

可以指定所需语言的charSet/numSet范围。Unicode范围可在Unicode图表。

API:

public static bool IsAlphaNumeric(this string stringToTest)
{
    // English
    const string charSet = "a-zA-Z";
    const string numSet = @"0-9";

    // Greek
    //const string charSet = @"\u0388-\u03EF";
    //const string numSet = @"0-9";

    // Bengali
    //const string charSet = @"\u0985-\u09E3";
    //const string numSet = @"\u09E6-\u09EF";

    // Hindi
    //const string charSet = @"\u0905-\u0963";
    //const string numSet = @"\u0966-\u096F";

    return Regex.Match(stringToTest, @"^(?=[" + numSet + @"]*?[" + charSet + @"]+)(?=[" + charSet + @"]*?[" + numSet + @"]+)[" + charSet + numSet +@"]+$").Success;
}

public static bool IsNumeric(this string stringToTest)
{
    //English
    const string numSet = @"0-9";

    //Hindi
    //const string numSet = @"\u0966-\u096F";

    return Regex.Match(stringToTest, @"^[" + numSet + @"]+$").Success;
}

public static bool IsAlpha(this string stringToTest)
{
    //English
    const string charSet = "a-zA-Z";

    return Regex.Match(stringToTest, @"^[" + charSet + @"]+$").Success;
}

用法:

// English
string test = "AASD121asf";

// Greek
//string test = "Ϡϛβ123";

// Bengali
//string test = "শর৩৮";

// Hindi
//string test = @"क़लम३७ख़";

bool isAlphaNum = test.IsAlphaNumeric();