[英]regular expression with 3 conditions with javascript and typescript
我想使用正則表達式驗證 xml 中的某個元素。
<ConfigOption>value</ConfigOption>
ConfigOption 的要求:
這就是我所擁有的。
(^[a-zA-Z0-9 _]*$)|(?:\??config=\d)|(^CONFIG\-\d$)
例如:“你好”應該沒問題。 'hello?config=20' 應該沒問題 'CONFIG-20' 應該沒問題。 'hello-' 應該失敗 'hello*' 應該失敗
有人可以修復我的正則表達式嗎? 當我測試“hello=”或“hello*”“https://regex101.com/”時,它仍然匹配。
這是我認為符合您要求的解決方案。 讓我知道它是否需要改進。
真正的區別在於添加前瞻(?=\W)
。
請注意,第三個條件是多余且不需要的。
const regex = /(?=\W)(^\b[a-zA-Z0-9 _]*)|(^[a-zA-Z0-9 _]*(?:\??[Cc][Oo][Nn][Ff][Ii][Gg][=,-]\d{2}))/; const tests = ['*HELLO', 'hello', 'hello=', 'hello*', 'hello?CONFIG-20', 'hello?CONFIG=20', 'config=20']; for (const test of tests) { console.log(regex.test(test)); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.