regex - JavaScript RegExp error - Nothing to repeat -
i have wrote regex match html text , 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 program, i'm getting error:
uncaught syntaxerror: invalid regular expression: nothing repeat
here 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 />"); }
with no regard whether pattern correct or not:
instead of using:
var re = new regexp("\+", "gmi");
use:
var re = /\+/gmi;
see mdn - creating regular expression.
if using string constructor, need string-escape backslashes:
var re = new regexp("\\+", "gmi");
as mentioned in comments, parsing html regexp not idea, especially in browser using javascript, since in context of giant html parser.
Comments
Post a Comment