-1

I think this is probably easy but I don't have the time to learn how to do it.

In a html file, I have a certain class of paragraph, let´s say:

<p class="footnote"></p>

The "p" tag is always followed by numbers, which increase by one in every instance. Let's say the first number is "43". I want the series of numbers to start from 1, so I need to substract 42 from all paragraphs.

For example, I would want to go from:

<p class="footnote">43. Lorem</p>
<p class="footnote">44. Ipsum</p>. 
<p class="footnote">45. Dolor</p>. 

to

<p class="footnote">1. Lorem</p>
<p class="footnote">2. Ipsum</p>. 
<p class="footnote">3. Dolor</p>. 

How can I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Sources say regex can't do math. http://stackoverflow.com/questions/5245087/math-operations-in-regex If you're working with InDesign you'll need to use a script. – user1754036 Sep 28 '15 at 23:28

2 Answers2

0

Get the text using javascript (though I'll be using jQuery), split the text, get the first element, and convert to an integer.

$(".footnote").each(function(){
    var text = $(this).text();  // Get text
    var num = text.split(/\s+/g)[0]; // Split by whitespace and get the first elem
    console.log(parseInt(num)); // Convert the elem to an int
});
BrockLee
  • 931
  • 2
  • 9
  • 24
0

If you're looking for a regex that'll handle <p class="footnote">43. Lorem</p> the answer is don't parse HTML with regex.

Assuming you've extracted the string 43. Lorem from a tag and you want to get a number out then it depends on your requirements:

To find any number: \d+

To find any number at the beginning: ^\d+

To find any number followed by a period: \d+\.

A more complete solution will require more details about the problem including the programming language you want to use.

Community
  • 1
  • 1
Alex Hall
  • 34,833
  • 5
  • 57
  • 89