Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript operator similar to SQL "like" [duplicate]

Tags:

javascript

Possible Duplicate:
Emulating SQL LIKE in JavaScript

Is there an operator in JavaScript which is similar to the like operator in SQL? Explanations and examples are appreciated.

like image 981
indu Avatar asked Aug 22 '09 05:08

indu


People also ask

Is there a like operator in JavaScript?

No, there isn't any. The list of comparison operators are listed here.

Is SQL like JavaScript?

SQL. js is a JavaScript library that allows you to create and query a relational database entirely in the browser. It uses a virtual database file stored in the browser memory, so it doesn't persist the changes made to the database.


2 Answers

You can use regular expressions in Javascript to do pattern matching of strings.

For example:

var s = "hello world!"; if (s.match(/hello.*/)) {   // do something } 

The match() test is much like WHERE s LIKE 'hello%' in SQL.

like image 83
cletus Avatar answered Sep 20 '22 11:09

cletus


No.

You want to use: .indexOf("foo") and then check the index. If it's >= 0, it contains that string.

like image 26
Noon Silk Avatar answered Sep 21 '22 11:09

Noon Silk