Skip to content
Michelle-PortSwigger edited this page Jan 9, 2024 · 5 revisions

Useful Regular Expressions

Removing the trailing / on a path

regex_replace(base.request.url.path, "/$", "")}

Trimming a string to a specific length

To trim the path variable to 42 characters, one can use: regex_replace({path}, "?<=^.{42}).*" , "")

Using regex_replace to extract data from the middle of a string

The logic behind this one is to use two regex_replace statements and feed the results of one into the other, so we can replace the beginning and end of the string with "" and leave ourselves with the bit in the middle we’re interested in.

For example, if we wanted to use this to find data in the middle of the request body:

  • First of all, trim off the end of the string using: {regex_replace({base.request.body},"<insert_your_regex_here>","")}

  • Then take that and use it as the string source for the next regex_replace and trim off the beginning of the string {regex_replace{{regex_replace({base.request.body},"<insert_your_regex_here>","")},"insert_your_regex_here>",""}

Using capture groups with placeholders

You can use capture groups in regex_replace, which could be useful for inserting details into the middle of a string, for example:

If you have the following URL

https://ginandjuice.shop/test1/test2/

You could use the following regex_replace statement to insert another level into the middle of the path

{regex_replace(base.request.url.path,"(^.*\/)(.*\/$)","mynewpath/")}

To get

https://ginandjuice.shop/test1/mynewpath/test2/

Clone this wiki locally