T-SQL UDF to replace replace base URLs
Your code looping over string functions doesn't seem like something SQL Server is optimized for.

Database systems are optimized for set based operations, not looping. I understand the confusion coming from procedural/Object Oriented languages, but in the end SQL isn't a programming language in it's classical form but a way to instruct an RDBMS what data you need.

Also, UDF's do all sort of weird things with execution plans, like hiding the actual cost in a plan, and often removing the option to choose a parallel plan for the optimizer. 

I wouldn't try to roll my own UDF for something like this.

The best way IMO would be to figure out a way to do it inline or set based. 
I don't have any sample data and expected output to work with but I could imagine a 

UPDATE [table] 
SET [url] = REPLACE([url], 'base_url', 'new_url') 
WHERE [url] LIKE '%&lt;regex&gt;%';


kind of construct

You might even do it in the application code (such as retrieving a datatable, fixing the data and saving the datatable again).

If you insist on doing it in a function I'd just either write a SQLCLR function or go and download the sqlsharp library and use that.

If you are worried about performance then read the article posted by the creator of the library comparing performance.

I highly doubt your implementation will work better or faster than a .NET regex solution.