3

I want to replace all unnecessary breaks in a string. I have written a Regex replace but it throws an error: SyntaxError: unterminated parenthetical

var str = "<H1>sdflk</H1><BR><BR><BR><BR><P>test</P><BR><BR><BR><BR>";
str.replace(/((</[a-zA-Z0-9]+>)(<BR>)+)/,"\$2");

But I don't see the missing parenthese.

Willem de Wit
  • 8,604
  • 9
  • 57
  • 90
  • 2
    Welcome to Stack Overflow! Please refrain from parsing HTML with RegEx as it will [drive you insane](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Jun 29 '12 at 08:51
  • I edited the question. I used $1 instead of $2 – Willem de Wit Jun 29 '12 at 09:27

1 Answers1

3

You must add \ before / in the re:

/((<\/[a-zA-Z0-9]+>)(<BR>)+)/

Another option is to use RegExp:

re = new RegExp("((</[a-zA-Z0-9]+>)(<BR>)+)");
"<H1>sdflk</H1><BR><BR><BR><BR><P>test</P><BR><BR><BR><BR>".replace(re,"\$1");
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144