2017-01-10 33 views
-1

鑑於三個URL之前和主要表達後排除:正則表達式 - 串兩者

  • http://stackoverflow.com/questionsss/3003310 - >真
  • http://stackoverflow.com/questionsss/3003310?s=1 - >假(S = 1實測值)
  • http://stackoverflow.com/questionsss?s=1&N=3003310 - >假(S = 1找到)

如何在JS中編寫正則表達式,如果在URL中找到3003310,則返回true;如果在URL中找到s = 1,則返回false。

謝謝!

回答

0

或許真的like this

^(?=.*3003310)(?!.*\bs=1\b).* 

積極的前瞻和負一層,每個條件。 \b圍繞s=1的單詞邊界可防止誤報,例如sass=1s=100

+0

感謝數百萬。它充當魅力。 – PeterPan

0

REGEXP:

(.*\?s=1.*) 

ü應該做的方法,如果結果是等於正則表達式匹配。它將返回FALSE否TRUE。

原始文本:

http://stackoverflow.com/questionsss/3003310 -> true 
http://stackoverflow.com/questionsss/3003310?s=1 -> false (s=1 found) 
http://stackoverflow.com/questionsss?s=1&N=3003310 -> false (s=1 found) 

結果:

http://stackoverflow.com/questionsss/3003310?s=1 -> false (s=1 found) 
http://stackoverflow.com/questionsss?s=1&N=3003310 -> false (s=1 found) 

算法:

Print(!OriginalText.equals(MethodCheck(Result)) // if empty. it will be TRUE 

參見:https://regex101.com/r/335HNW/1

+0

感謝您的幫助。不過,這不是我想要的。 – PeterPan