RegEx for Javascript to allow only alphanumeric

前端 未结 18 1535
轻奢々
轻奢々 2020-11-22 15:13

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just w

18条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 15:24

    It seems like many users have noticed this these regular expressions will almost certainly fail unless we are strictly working in English. But I think there is an easy way forward that would not be so limited.

    1. make a copy of your string in all UPPERCASE
    2. make a second copy in all lowercase

    Any characters that match in those strings are definitely not alphabetic in nature.

    let copy1 = originalString.toUpperCase();
    let copy2 = originalString.toLowerCase();
    for(let i=0; i

    Optionally, you can also detect numerics by just looking for digits 0 to 9.

提交回复
热议问题