-6

I'm trying to create a simple function that checks if there are foreign characters in my string. Essentially, this is what I'm trying to get:

var str_1 = "отправка.py" // should return true
var str_2 = "bla.py" // should return false
var str_3 = "bla23.py" // should return false
var str_4 = "bla23_.py" // should return false

I want to make sure that there aren't any foreign characters, while still making sure that I allow everything else (characters like "_", "-", ...).

I'm just trying to avoid the Russian, Mandarin, etc.. alphabets.

Michael
  • 503
  • 8
  • 18

2 Answers2

0

You are looking only for ASCII. So something like:

!/^[\x00-\x7F]*$/.test('отправка.py');  // => true
!/^[\x00-\x7F]*$/.test('bla.py');  // => false
Blake Simpson
  • 1,078
  • 6
  • 10
0

Code

See regex in use here

[^ -~]

Alternatively: [^\x00-\x7F] (which seems to have already been posted by @BlakeSimpson)

Usage

const r = /[^ -~]/
const a = [
  "отправка.py",
  "bla.py",
  "bla23.py",
  "bla23_.py"
]

a.forEach(function(s) {
  if(r.exec(s) !== null) {
    console.log(s)
  }
})

Explanation

[^ -~] Matches everything that's not from the space character (DEC 32) to the tilde ~ symbol (DEC 126) - which are all the visible ASCII characters.

Also, note that I don't use the g modifier. This is intentional as the OP is only asking to check whether or not there are foreign characters in the string. That means that so long as 1 character exists in the string that meet those requirements it should be matched (no need to match more than one).

ASCII Table

ctwheels
  • 21,901
  • 9
  • 42
  • 77