Pakistan Special
Arabic-Script Text Run Regex
Pakistani data often mixes English with text in the Arabic script, which is used to write Urdu and several regional languages. This pattern uses a Unicode property escape to match one or more characters whose script is Arabic, and the u flag is required for the escape to work in JavaScript. Use it to detect whether a field contains that script, to split mixed text into script runs, or to route messages to the right font or keyboard layout. Spaces are not part of the script, so each word becomes its own match; add \s to a class if you want whole phrases.
Pattern (JavaScript)
/\p{Script=Arabic}+/uMatches a character with Unicode property {Script=Arabic}, one or more times.
Examples
Matches
- ✓ text ابج mixed
- ✓ اب
- ✓ Ali Test درز
Does not match
- ✗ English only
- ✗ 12345
- ✗ Ali Test
Try it
3 matches (JavaScript engine)
text ابج mixed اب Ali Test درز English only 12345 Ali Test
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\p{Script=Arabic}+/gu;
const text = "text ابج mixed";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
ℹ Python's built-in re module has no \p{Script=...} support. This range covers the main Arabic block; the third-party regex module supports \p{Script=Arabic}.
import re
pattern = re.compile(r'[\u0600-\u06FF]+')
text = 'text ابج mixed'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)- Flag(s) u not available in this language and were left out.
PHP
<?php
$pattern = '/\p{Script=Arabic}+/u';
$text = 'text ابج mixed';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\p{Script=Arabic}+", Pattern.UNICODE_CASE);
String text = "text ابج mixed";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ Go's RE2 syntax names scripts directly, so \p{Arabic} replaces \p{Script=Arabic}.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\p{Arabic}+`)
text := "text ابج mixed"
fmt.Println(re.FindAllString(text, -1))
}- Flag(s) u not available in this language and were left out.
C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\p{Script=Arabic}+");
var text = "text ابج mixed";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}- Flag(s) u not available in this language and were left out.
Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"\p{Script=Arabic}+").unwrap();
let text = "text ابج mixed";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Flag(s) u not available in this language and were left out.
- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\p{Script=Arabic}+/
text = 'text ابج mixed'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matches- Flag(s) u not available in this language and were left out.
Frequently asked questions
- Why is the u flag required?
- Unicode property escapes such as \p{Script=Arabic} only work in JavaScript when the u or v flag is set. Without it, the pattern is a syntax error or matches the wrong thing.
- Does it match Arabic-script digits too?
- Script properties follow the Unicode data files, and some digits and punctuation marks are assigned to the Common script instead. Test the exact characters in your data, or add them to a class alongside the property.
- Why use a script property instead of a character range?
- The Arabic script spans several Unicode blocks, including supplements and presentation forms. The property covers all of them, while a single range such as U+0600 to U+06FF covers only the main block.