-2

I have the search string "sp".

In my database I have product titles that contain html tags.

Example 1:

I am an <span class="bold">examplatory</span> string.

Example 2:

I am a very beautiful spoon.

I want to make sure a result does not get shown if the search string is contained inside the html tag. Hence: I want example 1 not to show, because here "sp" is contained inside an html tag, but only example 2.

How can I check if the search string "sp" is a string part of an html tag?

Max
  • 832
  • 1
  • 14
  • 33
  • Even _"I am an"_ will be part of some tag.. – Rayon Jun 20 '16 at 13:46
  • Do you mean you want to find elements that have a tag name that includes your search string? Or where the search string appears anywhere between the `<` and `>` including in attribute names and values? Or searching in element content too? – nnnnnn Jun 20 '16 at 13:48
  • theString.indexOf('sp') > -1 ? Not sure how you get the string but probably some jQuery so use outerHtml() – nurdyguy Jun 20 '16 at 13:51
  • @nnnnnn I just specified my post. – Max Jun 20 '16 at 13:56
  • @Max: You do not want to run a regex on an HTML code unless you know what you are doing. The general suggestion is: parse the HTML, get the text value you need, run the regex to obtain the pattern you need. If you run a search, only run it on *text* nodes. – Wiktor Stribiżew Jun 20 '16 at 13:58
  • Possible duplicate of [Check if a string is html or not](http://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not) – Bram Vanroy Jun 20 '16 at 14:27

1 Answers1

0

If it's really necessary, you can use a negative lookahead, which will help you not to match end tags, for example.

The example bellow will match a text with sp unless it has a closing tag (>) after.

sp(?!.*>)

But @Wiktor's comment on your post is more valid, since the better way of avoid matching HTML tags on a regex is parsing the HTML.

gender_madness
  • 1,080
  • 11
  • 13