regsub

STRINGregsubSTRINGinputSTRINGpatternSTRINGreplacement

Available in all subroutines.

Replaces the first occurrence of pattern, which is a Perl-compatible regular expression, in input with replacement. If no match is found, no replacement is made. Calls to regsub do not set re.group.*.

This function may fail to make a replacement if the regular expression recurses too heavily. Such a situation may occur with lookahead and lookbehind assertions, or other recursing non-regular expressions. In this case, fastly.error is set to EREGRECUR.

This function is not prefixed with the std. namespace.

Errors

This function may fail to make a replacement if the regular expression recurses too heavily. Such a situation may occur with lookahead and lookbehind assertions, or other recursing non-regular expressions. In this case, fastly.error is set to EREGRECUR.

Example

The following example deletes any query string parameters.

set req.url = regsub(req.url, "\?.*$", "");

Try it out

regsub is used in the following solution recipes. Recipes apply VCL to real-world use cases and can be deployed as-is, or adapted for your own service. See the full list of recipesfor more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Relative date insertion using ESI

Generate relative time datelines like "3 minutes ago" at the Edge instead of in JavaScript or at origin. Better caching, faster rendering, fewer reflows.

Search and replace in strings

Use regular expression substitution functions (regsub) to map paths, strip extraneous slashes, and more.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.