1

I've just discovered RegEx and using Dreamweaver to try and update hundreds of pages on our intranet. The paths have one folder in common for my search - e.g. "hawww"

<a href="http://home/hawww/test1.asp">line1</a>
<a href="http://home/hawww/sample.html">page3</a>
<a href="hawww.html">line3</a>
<a href="www.hawww/test2.html">line4</a>

I would want to change all of those links to something like:

<a href="sample.html">

I can change them where the link starts with the same text, but not where it doesn't - if that makes sense. Would appreciate any help.

Thanks

Jonathan

  • So you would like to replace all links with the substring hawww with links to sample.html? – MShah Apr 15 '14 at 20:19
  • take a look at this python example and utilize the RegEx in a similar way http://stackoverflow.com/questions/11331982/how-to-remove-any-url-within-a-string-in-python – MethodMan Apr 15 '14 at 20:20
  • [These things are harder than they look](http://stackoverflow.com/a/4234491/471272). – tchrist Jun 06 '14 at 22:42

1 Answers1

0

Answer

I did a quick and dirty test. You can run these through vim or whatever RegEx tool you use. Essentially, the search regex should be this:

/href=\".*?hawww.*?"/g

Explanation

.*? means search for anything, but don't use greedy matching. The g at the end is an optional flag for global matching.

Test Case

Here is a test JS script I used.

var test = ['<a href="http://home/hawww/test1.asp">line1</a>',
  '<a href="http://home/hawww/sample.html">page3</a>',
  '<a href="hawww.html">line3</a>',
  '<a href="www.hawww/test2.html">line4</a>'];

var fixed = test.map(function(html) {
    return html.replace(/href=\".*?hawww.*?"/, 'href="simple.html"');    
});

console.log(fixed);

Mass Change

You can use a tool like sed to perform all the changes on your files. However, I recommend backing up and doing some testing afterwards before committing the changes. There may be some odd use cases where hawww is used in a link and you want to keep it there.

sed -i 's/href=\".*?hawww.*?"/href="simple.html"/g' /home/user/*.html
Pete
  • 3,141
  • 3
  • 18
  • 41