I need a regex to run against strings like the one below that will convert absolute paths to relative paths under certain conditions.
<p>This website is <strong>really great</strong> and people love it <img alt="" src="http://localhost:1379/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif" /></p>
Rules:
-
If the url contains “/Content/” I
would like to get the relative path -
If the url does not contain
“/Content/”, it is an external file,
and the absolute path should remain
Regex unfortunatley is not my forte, and this is too advanced for me at this point. If anyone can offer some tips I’d appreciate it.
Thanks in advance.
UPDATE:
To answer questions in the comments:
- At the time the Regex is applied, All urls will begin with “http://”
- This should be applied to the src attribute of both img and a tags, not to text outside of tags.
You should consider using the Uri.MakeRelativeUri method – your current algorithm depends on external files never containing “/Content/” in their path, which seems risky to me. MakeRelativeUri will determine whether a relative path can be made from the current Uri to the
srcorhrefregardless of changes you or the external file store make down the road.