1

Using textpad only, what regexp will find & amps; or &'s inside an href and replace them temporarily (even though I know the correct url encoding would have them be left as entities).. with a key that can be used later after TIDYING up. What I have now finds the first instance, but url params with multiple &'s don't get cleaned right, and doesn't account for items that are properly encoded with & amp; s already.

Here's what I have:

FIND WHAT: \(<a href="[^"]+?\)&

REPLACE WITH: \1-AMP-

What I want is something that will take this:

<A HREF="http://domain/boo.php?dross1=foo&dross2=bar&amp;dross3=baz&dross4=alpha&dross5=beta&amp;dross6=delta">

and turn it into this:

<A HREF="http://domain/boo.php?dross1=foo-AMP-dross2=bar-AMPENT-dross3=baz-AMP-dross4=alpha-AMP-dross5=beta-AMPENT-dross6=delta">

That way I can quickly search and replace to revert them back to their initial sate afterwards since this is only for validating, not for saving.

Thanks!

HansOg
  • 49
  • 5
  • I think it's problematic to narrow down only replacing ampersands *within links* - it can be done with lookaheads, but I don't know if textpad supports those. – mathematical.coffee Mar 09 '12 at 02:09
  • i could do two sep actions if that simplifies it? but yeah, both would have to be just in links, otherwise inline js (sigh to that) gets && biffed and so does & ampeveryotherentity; – HansOg Mar 09 '12 at 03:27

1 Answers1

0

I'm not sure about how this would be achieved in textpad, however in powershell this could be done as:

$String ='<A HREF="http://domain/boo.php?dross1=foo&dross2=bar&amp;dross3=baz&dross4=alpha&dross5=beta&amp;dross6=delta">'
$String -replace '(?<=<a[^<>]*href="[^"<> ]*?)&amp;',  "-AMP-"

yields

<A HREF="http://domain/boo.php?dross1=foo&dross2=bar-AMP-dross3=baz&dross4=alpha&dross5=beta-AMP-dross6=delta">

Dissecting the regex:

  1. The look around (?<= .... ) first validates that you're a anchor tag
  2. moves through that tag until it finds the href name
  3. moves through the string of non ", <, >, space characters looking for &
  4. All & strings are then found and replaced.
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43