DD
DevDash

Regex for ASCII Only String

Regex Pattern

^[\x00-\x7F]+$

String containing only ASCII characters (0-127)

Quick Answer

The regex pattern for ascii only string is `^[\x00-\x7F]+$`. String containing only ASCII characters (0-127). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
Hello World✓ Matches
abc123!@#✓ Matches
ASCII only✓ Matches
Café✗ No match
üöä✗ No match
你好✗ No match

Code Examples

javascript

const regex = /^[\x00-\x7F]+$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[\x00-\x7F]+$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[\x00-\x7F]+$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[\x00-\x7F]+$/', $value)) {
    echo "valid";
}

java

String pattern = "^[\\x00-\\x7F]+$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.