2

I have a directory full of Classic ASP legacy code, and almost all files have something similar to this:

<input type="hidden" name="driverPayment" value="<% =driverPayment %>">

Then later in the code, some JavaScript is running, and doing the following:

var x = document.getElementById('driverPayment')

This works fine in IE, but of course doesn't work anywhere else because there is no ID attribute defined.

The fix is to go through the 1770 files and manually add an ID attribute that matches the name property on the input. So make it like so:

<input type="hidden" name="driverPayment" id="driverPayment" value="<% =driverPayment %>">

Is there a way I can automate this process by using the logic below:

  1. Get input element with a name attribute
  2. If input has id attribute, move to next input
  3. Else add an ID attribute to the input, and give it a name matching the value of the name attribute

I'd like to do this for the 1770 Classic ASP files I have. I am using Sublime Text.

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • 2
    Could you try this regex ? `]*\bid="[^"]*")[^>]*name="([^"]*)"\K` and replace with `LiteralSpaceHereid="$1"`. [See demo](http://regex101.com/r/jQ3hR5). Also don't forget to try something :-) – HamZa Dec 03 '13 at 16:42
  • Thanks HamZa, your Demo shows exactly what I want to achieve, but how do I apply this in Sublime? Is it like Andy suggested? Or would yours be different? – J86 Dec 04 '13 at 11:08
  • 2
    Why don't you try it out ? – HamZa Dec 04 '13 at 11:23
  • Cause I'm an idiot sometimes :) Just tried it and I get no matches found! – J86 Dec 04 '13 at 11:48
  • Join me in the [regex chatroom](http://chat.stackoverflow.com/rooms/25767/regex). I'm busy now, I'll get back to you later. I hope I will find you there ... – HamZa Dec 04 '13 at 11:49

1 Answers1

2

You can use regex to match. My regex isn't great but the following should work. Happy for others to improve on it. I used some regex from this question.

Right Click project folder

Choose "Find in folder" option

Find and replace options appear at bottom of screen. Select the regex option (far left).

regex-find-and-replace

Enter

<(?:input)\s+(?=[^>]*\bname\s*=)(?![^>]*\bid\s*=)[^>]*>?(name="driverPayment")

in Find field

Enter

id="driverPayment" name="driverPayment" 

in Replace field

Click Replace

Community
  • 1
  • 1
Andy Davies
  • 1,456
  • 9
  • 13
  • `driverPayment` was just an example! it won't always be that! – J86 Dec 04 '13 at 11:05
  • @Ciwan you can run the find and replace operation a number of times, changing the name each time. If you search with just <(?:input)\s+(?=[^>]*\bname\s*=)(?![^>]*\bid\s*=)[^>]*> you'll find all the input fields that don't have an id. – Andy Davies Dec 04 '13 at 14:31
  • Thanks Andy, this is still too much work. HamZa's solution worked better. Put this `]*\bid="[^"]*")[^>]*name="([^"]*)"\K` in your Find field, and `id="$1"` in your replace field, it does it all for you :) Thanks @HamZa – J86 Dec 04 '13 at 15:21