[英]Regular Expression Match - Javascript
我正在嘗試編寫一個與此匹配的RegEx:
/Admin/doctemplate/edit/-537743660332489375
並且末尾的ID號可以更改。 也可以像這樣匹配一個單獨的RegEx:
/Admin/doctemplate/-537743660332489375/edit
我努力了:
/Admin/doctemplate/edit/[-0-9]+/
您需要轉義正斜杠。 然后,您可以將數字類\\d
用於數字( [-0-9]+
也將與0-4-6-58---458
匹配,我認為您並不想要)。
1. \\/Admin\\/doctemplate\\/edit\\/-\\d+
2. \\/Admin\\/doctemplate\\/-\\d+\\/edit
我強烈建議將regexr與regex混淆。
這是這兩種情況的正則表達式:
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/
有兩個例子:
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/.test(yourString1) ->true
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/.test(yourString2) ->true
如果要多次使用測試,最好將其設置為變量:
var re= new RegExp("^/Admin/doctemplate/(edit/-?\d+|-?\d+/edit)$")
re.test(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.