我正在構建一個 Web 框架,而 REGEX 今天真的是敵對的。我不喜歡用尖括號格式化自定義引數的 django 方式
url/<param>/... or
<str:token>/
我更喜歡 js 和其他程式處理這個的方式
name/:token/:another_param
嘗試了 45 分鐘后,我放棄了。我想只允許像a-zA-Z0-9_:/. 這里的主要問題是我不想允許這樣的遞回冒號
:::id
這些是我想匹配的字串
empty string (although I could check prior matching)
/
:id
/:id
/:id/
person/:name/:id/:token_person2/image
person////
////
有人可以幫助我嗎?
uj5u.com熱心網友回復:
你不想匹配::,顯然也不是:/
您可以做的是使用單個否定前瞻來斷言這兩個字串不會出現。
^(?![/\w:]*:[:/])[/\w:]*$
解釋
^字串的開始(?![/\w:]*:[:/])::負前瞻,不斷言或:/向右斷言[/\w:]*可選擇重復匹配/,\w(單詞字符)或:$字串結束
const regex = /^(?![/\w:]*:[:/])[/\w:]*$/;
[
"",
"/",
":id",
"/:id",
"/:id/",
"person/:name/:id/:token_person2/image",
"person////",
"////",
":::id",
"person/:/name",
].forEach(s =>
console.log((regex.test(s) ? "" : "No ") `Match --> '${s}'`)
)
查看正則運算式演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531743.html
標籤:javascriptPythonhtml正则表达式网络
上一篇:如何在Ruby中每次迭代后遍歷陣列并洗掉第一個元素?
下一篇:UBOOT編譯--- include/config.h、 include/autoconf.mk、include/autoconf.mk.dep、u-boot.cfg(三)
