[英]Regular Expression Match - Javascript
I am trying to write a RegEx that will match this: 我正在尝试编写一个与此匹配的RegEx:
/Admin/doctemplate/edit/-537743660332489375
And the id number at the end can change. 并且末尾的ID号可以更改。 Also a separate RegEx to match like so: 也可以像这样匹配一个单独的RegEx:
/Admin/doctemplate/-537743660332489375/edit
I have tried: 我努力了:
/Admin/doctemplate/edit/[-0-9]+/
You need to escape the forward slashes. 您需要转义正斜杠。 Then you can use the digit class \\d
for the numbers ( [-0-9]+
would also match 0-4-6-58---458
, which I don't think you want). 然后,您可以将数字类\\d
用于数字( [-0-9]+
也将与0-4-6-58---458
匹配,我认为您并不想要)。
1. \\/Admin\\/doctemplate\\/edit\\/-\\d+
1. \\/Admin\\/doctemplate\\/edit\\/-\\d+
2. \\/Admin\\/doctemplate\\/-\\d+\\/edit
2. \\/Admin\\/doctemplate\\/-\\d+\\/edit
I highly recommend regexr for messing with regex. 我强烈建议将regexr与regex混淆。
here is the regex for both cases: 这是这两种情况的正则表达式:
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/
with your two example: 有两个例子:
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/.test(yourString1) ->true
/^\/Admin\/doctemplate\/(edit\/-?\d+|-?\d+\/edit)$/.test(yourString2) ->true
if you want to use the test multiple times, better make it as a variable: 如果要多次使用测试,最好将其设置为变量:
var re= new RegExp("^/Admin/doctemplate/(edit/-?\d+|-?\d+/edit)$")
re.test(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.