开发者

regex for non english character [duplicate]

开发者 https://www.devze.com 2023-02-07 12:43 出处:网络
This question already has answers here: 开发者_StackOverflow社区Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago.

Possible Duplicate:

reject if file name contains non english character

how to check file name contain non english character like Cisco-S11-POA1800005815-Inv04736851-100919重做没有.pdf i need to avoid non english character


// Disallow anything but a-z, 0-9, underscore, period, hyphen, and comma
var disallowed = /[^a-z0-9_.,-]/i;
if (disallowed.test( myFileName )){
  // do something here
}

Add to the list of allowed characters as you see fit, but ensure that the hyphen is at the end of the list, or else escape it with \-.


The proper answer depends on knowing your character set.

But a regular expression like this:

var re = /[\x80-\xFF]/;
if (re.match(...)) {
    alert("File name contains non-ASCII character!");
}

is a quick answer.

Note that it will also complain if your file name contains any special symbols.

Maybe a whitelist, saying only the allowed characters, is a better option, something like this?

var re = /[^A-Za-z0-9_,.-]/;
if (re.match(...)) {
    alert("File name contains non-ASCII character!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号