亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在正則表達式中組合 2 個條件

在正則表達式中組合 2 個條件

Cats萌萌 2023-07-06 15:20:36
我想創建一個正則表達式來檢查非 ASCII 字符和一些特殊字符。 /^[\x00-\x7F]+$/    '[^{}$]*'這些條件單獨起作用,但當我將它們組合起來時則不起作用。嘗試過/^([\x00-\x7F]|[^{}$]*)$/   but failing.任何幫助將非常感激。提前致謝。
查看完整描述

2 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

^[\x00-\x7F]+$- 匹配完全由 ASCII 字符組成的字符串。

[^{}$]*?匹配零個或多個不同于{,}和 的字符$

因此,第二條規則匹配任何字符串。如果這是您的意圖,只需使用第一個正則表達式。

如果您想匹配任何純 ASCII 字符串(不包括 ){}$使用

^(?=[\x00-\x7F]+$)[^{}$]*$


解釋

--------------------------------------------------------------------------------

? ^? ? ? ? ? ? ? ? ? ? ? ? the beginning of the string

--------------------------------------------------------------------------------

? (?=? ? ? ? ? ? ? ? ? ? ? look ahead to see if there is:

--------------------------------------------------------------------------------

? ? [\x00-\x7F]+? ? ? ? ? ? ?any character of: '\x00' to '\x7F' (1 or

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?more times (matching the most amount

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?possible))

--------------------------------------------------------------------------------

? ? $? ? ? ? ? ? ? ? ? ? ? ? before an optional \n, and the end of

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?the string

--------------------------------------------------------------------------------

? )? ? ? ? ? ? ? ? ? ? ? ? end of look-ahead

--------------------------------------------------------------------------------

? [^{}$]*? ? ? ? ? ? ? ? ? any character except: '{', '}', '$' (0 or

? ? ? ? ? ? ? ? ? ? ? ? ? ?more times (matching the most amount

? ? ? ? ? ? ? ? ? ? ? ? ? ?possible))

--------------------------------------------------------------------------------

? $? ? ? ? ? ? ? ? ? ? ? ? before an optional \n, and the end of the

? ? ? ? ? ? ? ? ? ? ? ? ? ?string


查看完整回答
反對 回復 2023-07-06
?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

看起來你的正則表達式的這一部分有問題[\x00-\x7F]。我已經嘗試過以下方法并且有效。

^([[:ascii:]]+$|[^{}$]*)$


查看完整回答
反對 回復 2023-07-06
  • 2 回答
  • 0 關注
  • 243 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號