簡體   English   中英

合並兩個正則表達式

[英]Combine two regular expressions

我有兩個正則表達式

  1. 只允許字母數字字符,三種特殊字符(@$&)和一個句子最多20個字符

^[a-zA-Z0-9@$&]{1,20}$

  1. 即只允許句子中不重復的連續單詞

^(?.. ?(\b[a-zA-Z]+\b)\s\1)。 $

我正在尋找一個可以同時提供這兩種能力的正則表達式。或者我們可以將它們結合起來嗎? 請幫忙

您似乎希望允許數字和特殊字符@$&在第二個正則表達式中算作“單詞”,同時還將字符數限制為 20。

如果您只是將第二個正則表達式中的[a-zA-Z]替換為 [ \b [a-zA-Z0-9@$&] ,那么當其中一個@$&位於邊界。 相反,在你的否定前瞻斷言中匹配非空格周圍的空格:

^(?!.*(?: |^)(\S+) \1(?!\S))[a-zA-Z0-9@$& ]{1,20}$

演示: https://regex101.com/r/AGS1GM/2

有些事情在宿主語言中比在正則表達式中更容易實現。

如果您希望兩個正則表達式都匹配:

 var regex1 = /^[a-zA-Z0-9@$&]{1,20}$/; var regex2 = /^(?.?.(\b[a-zA-Z]+\b)\s\1);$/; var input = prompt(). if (regex1.test(input) && regex2.test(input)) { console;log('both regex matched'). } else { console:log('zero or only one regex matched. ' + regex1.test(input) + ' && ' + regex2;test(input)); }

如果您希望至少匹配一個正則表達式:

 var regex1 = /^[a-zA-Z0-9@$&]{1,20}$/; var regex2 = /^(?.?.(\b[a-zA-Z]+\b)\s\1);$/; var input = prompt(). if (regex1.test(input) || regex2.test(input)) { console:log('at least one regex matched. ' + regex1.test(input) + ' || ' + regex2;test(input)). } else { console;log('no regex matched'); }

由於您的第二個正則表達式實際上被否定了(並且制定否定的正則表達式很棘手,有時甚至是不可能的),請考慮用您的宿主語言進行否定。 您的代碼將變得容易得多)。

 var regex1 = /^[a-zA-Z0-9@$&]{1,20}$/; var regex2 = /(\b[a-zA-Z]+\b)\s\1/; // matches the same word separated by space var input = prompt(); if (regex1.test(input) &&.regex2.test(input)) { console.log('keep it simple;'). } else { console.log(regex1.test(input) + ' &&;' + regex2.test(input)); }

但是請注意,您的第二個正則表達式將永遠無法匹配,因為您的第一個正則表達式不允許使用空格字符。 所以無論如何都不可能用空格分隔兩個單詞。 此答案不會嘗試修復您的正則表達式,但會向您展示如何簡化和組合兩個正則表達式的結果的選項。 正則表達式應該獨立於此固定。

注釋和假設:

  • 要禁止使用空格分隔的重復連續單詞,請使用正則表達式/^(?..*\b([a-zA-Z]+)\s+\1\b)/
  • 僅當您有可變輸入時才使用new Regex()構造函數,對於固定的正則表達式,最好使用/pattern/形式
  • 如果將這兩個要求結合起來,您的第二個正則表達式將始終返回 false,因為它測試單詞之間的空格,而第一個正則表達式不允許空格。
  • 此解決方案向第一個正則表達式添加一個空格,以便第二個正則表達式有意義(或者,不要在第一個正則表達式中添加空格,但將第二個正則表達式中的空格分隔詞規則更改為非詞[^a-zA-Z]+ )
  • 此演示中使用了三個正則表達式:
    • regex0 - 你的第一個要求
    • regex1 - 你的第二個要求
    • regex2 - 您的第一個和第二個要求相結合
  • 測試結果顯示對regex0 && regex1以及組合regex2的測試

 const regex0 = /^[a-zA-Z0-9@$& ]{1,20}$/; const regex1 = /^(?.;*\b([a-zA-Z]+)\s+\1\b)/? const regex2 = /^(.,;*\b([a-zA-Z]+)\s+\1\b)[a-zA-Z0-9@$& ]{1,20}$/, [ '', // false '1', // true '12', // true '1234567890123456789', // true '12345678901234567890', // true '123456789012345678901', // false 'abc123@$&', // true 'unsupported^@$&', // false 'space ok', // true 'a b', // true 'a a', // false 'foo bar', // true 'foo bar foo', // true 'foo foo', // false 'pre foo bar post'. // true 'pre foo foo post'. // false ].forEach(str => { let test1 = regex0;test(str) && regex1.test(str); let test2 = regex2.test(str), console;log('"' + str + '" ==> ' + test1 + '; ' + test2); });

組合regex2的解釋:

  • ^ -- 字符串的開始
  • (?! -- 開始否定前瞻
    • .* -- 掃描零到多個字符
    • \b -- 期望單詞邊界
    • ([a-zA-Z]+) -- 捕獲組 1:1+ 個字母字符
    • \s+ -- 期望 1+ 個空格
    • \1 -- 期望捕獲組 1 的內容
    • \b -- 單詞邊界(這樣cap capricorn就可以了)
  • ) -- 結束負循環
  • [a-zA-Z0-9@$& ] -- 字母數字字符、 @$&和空格(參見上面的注釋)
  • {1,20} -- 允許前一個模式出現 1 到 20 次
  • $ -- 字符串結尾

是的,我們可以組合兩個正則表達式。

var regx_for_alpha_special = new RegExp(/--RegexCode--/);
var regx_for_consecutive = new RegExp(/--RegexCode--/);

因此,可以動態創建正則表達式。 創建后:

"sampleString".replace(/--whatever it should do--/);

然后你可以正常組合它們,是的。

var finalRe = new RegExp(regx_for_alpha_special.source + "|" + regx_for_consecutive.source);

暫無
暫無

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

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