in this lecture, we're going to replace the placeholder with a regular expression.
our router must provide support for route parameters.
route parameters are written with curly brackets.
the goal is to replace these placeholders with regular expressions.
by doing so, we'll be able to extract the value in a url.
in order to perform this action, we must detect placeholders in our paths.
detecting placeholders can also be performed with regular expression.
let's use the regular expression 101 tool again.
if you lost the link to this tool, check out the resource section of this lecture for a link.
in the test string box, let's add our route as an example.
type the following: transaction transaction.
the 1st regular expression we'll write is to detect a placeholder in a path.
1st, let's change the delimiter to hash characters.
in the regular expression field, add a pair of curly brackets.
the 1st step is to search for opening and closing curly brackets.
if we were to remove the route parameter name, the regular expression would be able to find them.
however, that's not exactly what we want.
our route parameters are going to have names.
names can be anything a developer wants.
in that case, our expression should be able to select the entire segment from the beginning of the curly bracket to the closing curly bracket.
inside these curly brackets, add a pair of square brackets.
we've talked about square brackets before.
they're used to select a range of characters.
we want to be able to select any character between the curly brackets with one exception.
we don't want to select characters with the character.
this character indicates a new path segment.
we only want to select characters in the current path segment.
add this character to the square brackets.
if we look at the explanation, the tool states the expression will select this character.
its effect is the opposite of what we want.
at the beginning of the square brackets, add the caret character.
the caret character changes the behavior of the square brackets.
this character instructs the regular expression not to select the characters in this range.
taking a look at the explanation, the description has slightly changed.
it's stating that any characters in this range are not matched, which is what we want.
the last step is to select multiple characters.
as we know, regular expressions only select a single character.
our expression must be able to select all characters inside a pair of curly brackets.
after the square brackets, add the plus character.
if we look at the results, the transaction placeholder has been selected.
we've successfully written a regular expression for selecting placeholders.
let's try using it in our framework.
copy the expression to the clipboard.
next, open the router file in your editor.
we're going to update the add method.
before inserting a record into the routes array, we'll update the path by creating a variable called regexpath.
the value will be a function called preg replace.
the preg replace function performs a regular expression on a string.
on top of searching for a value, it'll replace the match with any value we'd like.
once again, we're going to replace the path passed into the router with a regular expression.
by doing so, we'll be able to extract values from the path.
the 1st argument to this function is the regular expression.
let's pass in our custom regular expression.
next, we must provide the value we'd like to use as a replacement.
the replacement value will be the regular expression without the hash and curly bracket characters.
surround the expression with a pair of parentheses.
keep in mind, we're going to allow for paths to support multiple parameters.
if that's the case, we'll need to extract multiple values from a single path.
we're using the same expression with parentheses instead of curly brackets.
parentheses create a group.
it's not uncommon for regular expressions to select multiple values.
by adding these characters, we'll be able to select multiple values from a single string.
the last argument to this function is the string to perform the regular expression on.
pass in the path variable.
afterward, let's add this new path to the record, in the array, at an item called regexpath set to the regexpath variable.
lastly, let's use our regular expression inside the dispatch method.
if you scroll to this method, there is a conditional statement to check the current path with a regular expression.
let's replace the variable in the 1st argument of the pragmatch function with the route regexpath variable.
after making those changes, we can try testing our work.
in the browser, try visiting the page to edit a transaction.
our application throws an error.
looking closely, the error states the edit view method does not provide an array called parameters.
that's great.
well, not really.
ideally, this route should render a form.
however, our router is able to find a route matching the url path.
previously, we would receive a blank page because our router couldn't find a match.
now that it's able to find a match, we'll be able to render a method.
as long as you get a similar error, you're good to go.
in the next lecture, let's extract the values from the path and pass it onto our controllers method.