0

When I am searching a string using the below jquery search method it is working fine, but when the strings have a '*' mark in them then it is not searching.

$(this).html().search(a)

this works fine until 'a' and '$(this).html().search(a)' does not have '*' asterisk mark. If 'a' and '$(this).html()' both are same string with * mark then it fails.

antnewbee
  • 1,779
  • 4
  • 25
  • 38

2 Answers2

0

serach() isn't a jquery function. It's a javascript function. It accepts regular expressions. According to regular expression, * is a special character that means 0 or more occurrences of the preceding character should be matched. Try to escape asterisks with: \\*

mauretto
  • 3,183
  • 3
  • 27
  • 28
  • Thanks mauretto, the problem is $(this).html() is inside a for loop so it's not constant. Is there a way it will escape automatically if there is an asterisk? – antnewbee Apr 07 '16 at 08:32
  • @antnewbee edit your question with the loop, the community will help you. – mauretto Apr 07 '16 at 08:57
0

you can use jQuery's text() to escape the output automatically.

check: http://api.jquery.com/text/ for examples. This is also related to your question: Escaping HTML strings with jQuery

Community
  • 1
  • 1
Ron
  • 5,900
  • 2
  • 20
  • 30