Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

javascript search() working problem

I have an jquery autosuggest plugin in which when i type ex. Korea then it show 2 results i.e. Korea (North) & Korea (South) but when i type Korea (, its not displaying any result where it had to show both Korea (North) & Korea (South).

After searching in the code for the error then i found a search() function which search for the type string in the results..

means: Korea ( in Korea (North) & Korea (South)

and i think this search function stops at bracket ( and i don't know why???

Please, suggest a search() function to remove this error..

code:::

str.search(query) != -1

where: str = one result at a time i.e. Korea (North) query = entered string i.e. Korea (

Upvotes: 0

Views: 222

Answers (2)

xzyfer
xzyfer

Reputation: 14135

search() is a regular expression function. The ( character has significant meaning in regular expression, hence why the search fails. If you escape the ( with a backslash it should work

str.search("Korea \(");

Upvotes: 0

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99909

The first parameter of search() is treated a regular expression, and Korea ( is an invalid regular expression. This is why "Korea (North)".search("Korea (") fails.

Use indexOf() instead:

str.indexOf(query) != -1

Upvotes: 1

Related Questions