0

I am trying to use a regular expression to just display 'Desired String' which is inside HTML code, can anyone see where I am going wrong?

alert("< TD>< B>< B>13COB251< /B> - Desired String< /B>< /TD>".replace(/(<[A-Z]*>)+13COB251(<\/[A-Z]*>)+ - ([a-z]*)(<\/[A-Z]*>)+/gi, "$3"))

Thanks!

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Josh
  • 1

3 Answers3

1

If you put your current code through Regexper you'll be given the following error:

Expected one of *, +, ?, {, {,, (, [, ., \, $, |, ) at line 1, column 22 (byte 22) after /(<[A-Z]>)+13COB251(<

You need to escape the / character by placing a backslash (\ before it):

/(<[A-Z]>)+13COB251(<\/[A-Z]>)+ - ([a-z])(<\/[A-Z]>)+/gi

Regex

However as you can see, this isn't really accurate. One of the best answers on StackOverflow will explain why you shouldn't parse HTML with regular expressions.

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

You need to escape the / characters in the pattern as that is the delimiter.

Then you need some more quantifiers. The pattern (<[A-Z]>)+ matches one or more HTML tags, but only those that has a single character tag name. You need (<[A-Z]+>)+ to match the <TD> tag also.

The pattern for the desired string also needs a quantifier, and the set needs to match more characters as the desired string doesn't contain only lower case characters.

alert("<TD><B><B>13COB251</B> - Desired String</B></TD>".replace(/(<[A-Z]+>)+13COB251(<\/[A-Z]>) - ([A-Za-z ]+)(<\/[A-Z]+>)+/gi, "$3"))

Demo: http://jsfiddle.net/RH6zc/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

You forgot to account for the whitespace inside the tags and in your desired string.

/(< *[A-Z]*>)+13COB251(< *\/[A-Z]*>)+ - ([a-z ]*)(< *\/[A-Z]*>)+/gi
   ^                    ^                    ^     ^
user123444555621
  • 148,182
  • 27
  • 114
  • 126