1 回答

TA貢獻1765條經驗 獲得超5個贊
如果我沒看錯的話,規則是:
必須至少有一個阿拉伯文字字符
必須僅包含阿拉伯文字、空格和
,.()-
如果是這樣,您想要:
對字符串中某個位置的阿拉伯字符進行正向預測,并結合
^
和$
錨點要求它們之間的所有內容都是阿拉伯語或.,()-
或空格
如果是這樣,您可以使用 Unicode 屬性轉義(ES2018+,在最新的 Firefox、Chrome/Chromium、Safari、Brave 和 Edge [v79+] 中受支持;或者您可以使用 http://xregexp.com 等庫) / ):
const rex = /^(?=.*?\p{Script_Extensions=Arabic})[- ().,\p{Script_Extensions=Arabic}]+$/u;
實例:
const rex = /^(?=.*?\p{Script_Extensions=Arabic})[- ().,\p{Script_Extensions=Arabic}]+$/u;
const shouldNotMatch = [" ", ",()-"];
const shouldMatch = ["??????????", ".()-,??????????", "??????????.(?)-,"];
for (const str of shouldNotMatch) {
const result = rex.test(str);
console.log(str, result, result ? "ERROR" : "Good");
}
for (const str of shouldMatch) {
const result = rex.test(str);
console.log(str, result, result ? "Good" : "ERROR");
}
添加回答
舉報