Replace Rule is commonly used ruleType provided by Requestly. As the word
replaceindicates it allows users to replace a string (or matching pattern) with another string in the url. It looks to be very simple but it can achieve very complex use cases. Lets discuss some of them one by one.
Situation: Suppose you are working on a web app which fetches content from your REST API available at
http://my-app-server.com/rules. You have cloned the API server, made some changes and running it locally on
http://localhost:4000/rules. Now, you want to test your app with the new changes done on API server.
Approach1: Change the domain in your App code to hit
localhost instead of
my-app-server. Once your testing is done revert this change.
Approach2: Use Replace Rule Define a Replace Rule to replace
http://localhost:4000 in all URLs. No need to change url inside your code. After your testing, just disable the rule. As simple as that.
Chrome Store Feedback Here is a review listed on Chrome Store about Replace Rule.
Change Query Parameter Values
You can change the values of query parameters present in Url. For example: Changing
?dl=1 in dropbox Urls downloads the file directly without going to the usual preview page. Such a simple use case of Replace Rule.
Fix broken/updated Urls
Often you may find that links to some documentation or blog posts are changed but you have developed a typing habbit of old urls. Well, you can use replace rule to achieve that use case.
Pattern Matching in Replace Rule
With all the use cases mentioned above, we know Replace Rule can be used to replace a string with another string in a Url. Apart from it, Replace Rule can also match a
JS Based Regex in the Urls and you can replace them with another string. Please note in case of Regex pattern, you can also use the values of group expressions in your regex and use them in final strings. For Example:
Example 1: Extract domain, pathName and extension for Urls ending with
- $1 is the value of 1st group expression. – DomainName
- $2 is the value of 2nd group expression – PathName but not file extension
- $3 is the value of 3rd group expression – File extension
.jpgin this case
Example 2: Strip out Query Parameters in a URL
Replace /(.+)(\?)(.+)/ig With $1
- $1 is the value of URL before “?”
- $2 matches “?”
- $3 is your query string
Checkout this Regex on regex101 and try your use cases.
You can use Regex 101 for creating your Regex and try out various scenarios. For any further questions, please drop us a comment or open a bug on Github.