1

I am trying to write a RegEx that will match this:

/Admin/doctemplate/edit/-537743660332489375

And the id number at the end can change. Also a separate RegEx to match like so:

/Admin/doctemplate/-537743660332489375/edit

I have tried:

/Admin/doctemplate/edit/[-0-9]+/

asked Jun 9, 2014 at 22:21

2 Answers 2

2

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(...)
answered Jun 9, 2014 at 22:30
Sign up to request clarification or add additional context in comments.

Comments

2

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).
1. \/Admin\/doctemplate\/edit\/-\d+
2. \/Admin\/doctemplate\/-\d+\/edit

I highly recommend regexr for messing with regex.

answered Jun 9, 2014 at 22:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.