1

I've got an HTML page with a long series of data in tabular format (2194 entries!), each starting with

<td id="foo"...> 

I need each id to be unique, and so would like to use a regular expression in Dreamweaver's find and replace function to do this. Basically, it would look like this before:

<td id="foo"...>
<td id="foo"...>
<td id="foo"...>

and, using the regular expression, changed to:

<td id="001"...>
<td id="002"...>
<td id="003"...>

Is this possible? And, if so, what's the syntax of the expression?

Thanks in advance!

razlebe
  • 7,134
  • 6
  • 42
  • 57
  • 1
    Can you change this code? For example, can you use a class instead of an ID, or are you locked into the code that's already there? – Justin Morgan - On strike Mar 15 '11 at 16:30
  • You can't do this using Dreamweaver's regex. You need to to this in a programming language where the regex replacement can be a function. – thirtydot Mar 15 '11 at 16:34
  • All of the ``'s already have classes, I'm afraid - it's for a CMS, which need the same class, but requires each editable tag to have a unique ID. – Andy Burgess Mar 15 '11 at 16:53

1 Answers1

0

Instead of using something like a simple find/replace from a text editor, I'd run it through some kind of HTML DOM script. Here's some jQuery-ish pseudocode:

var tdList = source.find('td.foo');

for (i = 0; i < tdList.length; i++) {
     tdList[i].attr('id', 'foo' + i.padWithZeroes(length:3));
}
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • Thank you for the code. I've tried to put it in the head of my HTML document, in ` – Andy Burgess Mar 15 '11 at 17:03
  • @Andy - It's just a pseudocode example, and it's based on jQuery. Just trying to give you an idea of what to do. It should be close to working jQuery code, but you'd have to include the jQuery JS framework, and the `'td.foo'` selects td elements of class "foo". You could try `'td#foo'`, but since multiple elements with id "foo" would break validation, I don't know if that would work. Since you know your code better than I do, I suggest you write your own script in whatever language you're comfortable with, based on the above idea. You could also use a server-side script to rewrite the files. – Justin Morgan - On strike Mar 15 '11 at 17:14