簡體   English   中英

使用多行匹配排除某些字符的正則表達式

[英]Regular expression for excluding some characters with multiline matching

我想確保用戶輸入的內容不包含<>&#類的字符,無論它是文本輸入還是textarea。 我的模式:

var pattern = /^((?!&#|<|>).)*$/m;

問題是,它仍然匹配文本區域中的多行字符串,例如

該文本匹配

盡管不應該這樣,因為這個字符<

編輯:

更清楚地說,我只需要排除&#組合,而不是&#

請提出解決方案。 非常感謝。

在這種情況下,我認為您不需要環視斷言。 只需使用否定的字符類:

var pattern = /^[^<>&#]*$/m;

如果您還不允許以下字符-[] ,請確保將其轉義或以正確的順序放置:

var pattern = /^[^][<>&#-]*$/m;

您可能不是在尋找Java中的m (多行)開關,而是s (DOTALL)開關。 不幸的是s在Javascript中不存在。

但是,可以使用[\\s\\S]模擬DOTALL的好消息 嘗試使用以下正則表達式:

/^(?![\s\S]*?(&#|<|>))[\s\S]*$/

要么:

/^((?!&#|<|>)[\s\S])*$/

現場演示

特定問題的替代答案:

anubhava的解決方案可以准確地工作,但是速度很慢,因為它必須在字符串中的每個字符位置進行負向超前查找。 一種更簡單的方法是使用反向邏輯。 即不是驗證的: /^((?!&#|<|>)[\\s\\S])*$/ 匹配,驗證/[<>]|&#/ 匹配。 為了說明這一點,讓我們創建一個函數: hasSpecial() ,它測試字符串是否具有特殊字符之一。 這是兩個版本,第一個使用anubhava的第二個正則表達式:

function hasSpecial_1(text) {
    // If regex matches, then string does NOT contain special chars.
    return /^((?!&#|<|>)[\s\S])*$/.test(text) ? false : true;
}
function hasSpecial_2(text) {
    // If regex matches, then string contains (at least) one special char.
    return /[<>]|&#/.test(text) ? true : false;
}

這兩個功能在功能上是等效的,但第二個功能可能要快得多。

請注意,當我最初閱讀此問​​題時,我將其誤解為確實要排除HTML特殊字符(包括HTML實體)。 如果真是這樣,那么下面的解決方案將做到這一點。

測試字符串是否包含HTML特殊字符:

看來OP希望確保字符串不包含任何特殊的HTML字符,包括: <>以及十進制和十六進制HTML實體,例如: &#160; &#xA0; 如果是這種情況,則解決方案可能還應該排除其他(命名)類型的HTML實體,例如: &amp; &lt; 等等。下面的解決方案排除了HTML實體的所有三種形式以及<>標記定界符。

這是兩種方法:(請注意,兩種方法都允許序列: &#如果它不是有效的HTML實體的一部分)。

使用正則表達式進行FALSE測試:

function hasHtmlSpecial_1(text) {
    /* Commented regex:
        # Match string having no special HTML chars.
        ^                  # Anchor to start of string.
        [^<>&]*            # Zero or more non-[<>&] (normal*).
        (?:                # Unroll the loop. ((special normal*)*)
          &                # Allow a & but only if
          (?!              # not an HTML entity (3 valid types).
            (?:            # One from 3 types of HTML entities.
              [a-z\d]+     # either a named entity,
            | \#\d+        # or a decimal entity,
            | \#x[a-f\d]+  # or a hex entity.
            )              # End group of HTML entity types.
            ;              # All entities end with ";".
          )                # End negative lookahead.
          [^<>&]*          # More (normal*).
        )*                 # End unroll the loop.
        $                  # Anchor to end of string.
    */
    var re = /^[^<>&]*(?:&(?!(?:[a-z\d]+|#\d+|#x[a-f\d]+);)[^<>&]*)*$/i;
    // If regex matches, then string does NOT contain HTML special chars.
    return re.test(text) ? false : true;
}

請注意,上述正則表達式利用了Jeffrey Friedl的“展開循環”效率技術,在匹配和不匹配的情況下都將非常快速地運行。 (請參閱他的正則表達式傑作:《 掌握正則表達式》(第3版)

使用負正則表達式的TRUE測試:

function hasHtmlSpecial_2(text) {
    /* Commented regex:
        # Match string having one special HTML char.
          [<>]           # Either a tag delimiter
        | &              # or a & if start of
          (?:            # one of 3 types of HTML entities.
            [a-z\d]+     # either a named entity,
          | \#\d+        # or a decimal entity,
          | \#x[a-f\d]+  # or a hex entity.
          )              # End group of HTML entity types.
          ;              # All entities end with ";".
    */
    var re = /[<>]|&(?:[a-z\d]+|#\d+|#x[a-f\d]+);/i;
    // If regex matches, then string contains (at least) one special HTML char.
    return re.test(text) ? true : false;
}

還請注意,我以JavaScript注釋的形式包含了每個(非平凡的)正則表達式的注釋版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM