-5

I have wrote a Regex to match HTML Text and it works in regex101.com.

<h2>Dollarkurs\sAktuell<\/h2><\/div><div[^>?]+><div><table>
<colgroup><col[^>?]+><col><col[^>?]+><\/colgroup><tbody><tr>
<td[^>?]+>Kurs<\/td><td[^>?]+>([^\s^<?]+)\s*<span[^>?]+>
(\+|-)?\d+\,?\d{0,2}%<\/span><span[^>?]+><\/span><\/td>
<\/tr><tr><td[^>?]+>Kurszeit<\/td><td[^>?]+>
(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?\sUhr
<\/td><\/tr><tr><td[^>?]+>Kursdatum<\/td><td[^>?]+>([0-9\.]+)<\/td>

The test String is:

<h2>Dollarkurs Aktuell</h2></div><div class='content'><div><table>
<colgroup><col width='50px'><col><col width='100px'></colgroup><tbody><tr>
<td class='bold'>Kurs</td><td class='textRight' colspan='2'>1,0947 <span class='distanceLeft right green'>
+0,58%</span><span class='distanceLeft right imageIconPriceGreen'></span></td>
</tr><tr><td class='bold' colspan='2'>Kurszeit</td><td class='textRight'>
16:00:00 Uhr
</td></tr><tr><td class='bold' colspan='2'>Kursdatum</td><td class='textRight'>28.04.2015</td>

But in the Program, I'm getting this error:

Uncaught SyntaxError: Invalid regular expression: 
Nothing to repeat

Here is my code:

var htmlTxt = "<h2>Dollarkurs Aktuell</h2></div><div class='content'><div><table><colgroup><col width='50px'><col><col width='100px'></colgroup><tbody><tr><td class='bold'>Kurs</td><td class='textRight' colspan='2'>1,0947 <span class='distanceLeft right green'>+0,58%</span><span class='distanceLeft right imageIconPriceGreen'></span></td></tr><tr><td class='bold' colspan='2'>Kurszeit</td><td class='textRight'>16:00:00 Uhr</td></tr><tr><td class='bold' colspan='2'>Kursdatum</td><td class='textRight'>28.04.2015</td>";

var re = new RegExp("<h2>Dollarkurs\sAktuell<\/h2><\/div><div[^>?]+><div><table><colgroup><col[^>?]+><col><col[^>?]+><\/colgroup><tbody><tr><td[^>?]+>Kurs<\/td><td[^>?]+>([^\s^<?]+)\s*<span[^>?]+>(\+|-)?\d+\,?\d{0,2}%<\/span><span[^>?]+><\/span><\/td><\/tr><tr><td[^>?]+>Kurszeit<\/td><td[^>?]+>(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?\sUhr<\/td><\/tr><tr><td[^>?]+>Kursdatum<\/td><td[^>?]+>([0-9\.]+)<\/td>", "gmi");

var result = re.exec(htmlTxt);

while (result != null)  {
  document.write("["+re.lastIndex+"] "+result);
  document.write("<br />");
 }
Kobi
  • 135,331
  • 41
  • 252
  • 292
Ying Style
  • 752
  • 2
  • 7
  • 25
  • 2
    Bring the entire question here. – ʰᵈˑ Apr 29 '15 at 07:42
  • 1
    Please post your relevant code and error messages here and do not link to other sites to describe your problem. – tjati Apr 29 '15 at 07:42
  • 3
    [Don't parse HTML with regex!](http://stackoverflow.com/a/1732454/418066) – Biffen Apr 29 '15 at 07:45
  • 1
    @Biffen That answer is funny, and not wrong per se, but there's nothing wrong with using regex to scrape values from expectable HTML. They are not trying to parse HTML with regex. – L3viathan Apr 29 '15 at 07:47
  • @L3viathan You and I are apparently of different opinions: I'd say there most certainly *is* something wrong with using regex to scrape HTML. Either the piece of HTML is *the exact same string* every time, in which case there's no need to scrape it at all, or (more likely) it will change over time, and sooner or later the regex won't work. – Biffen Apr 29 '15 at 07:54

1 Answers1

2

With no regard to whether the pattern is correct or not:

Instead of using:

var re = new RegExp("\+", "gmi");

use:

var re = /\+/gmi;

See MDN - Creating a regular expression.

If you are using the string constructor, you need to string-escape all backslashes:

var re = new RegExp("\\+", "gmi");

As already mentioned in the comments, parsing HTML with a regexp is not always a good idea, especially in the browser using JavaScript, since you are already in the context of a giant HTML parser.

Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • For getting Data of dollar exchange rate from the Web: http://www.finanzen.net/devisen/dollarkurs Is there any better idea? – Ying Style Apr 29 '15 at 08:14
  • @YingStyle - Of course. Look at this question: [How do I get currency exchange rates via an API?](http://stackoverflow.com/q/3139879/7586) – Kobi Apr 29 '15 at 08:17