I want to match /events/:eventId
or /events/:eventId/
with regex. :eventId
can be any alphanumeric string.
This is what I have at the moment
str.match(/events\/[A-Za-z0-9]+\/?/)
How can I make sure it doesn't match something like:
/events/das873jh/image
Update
For some reason, this code matches but puts random characters like a ,
at the end of the string when there's a match. Why?!
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "/events/ji893u";
var res = str.match(/^\/events\/[a-zA-Z0-9]+(\/)?$/);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>