tags:

views:

1294

answers:

7

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.

+6  A: 

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.

cletus
It's better to use `test` here (which should, by the way, be faster), since actual match result is not needed.
kangax
+2  A: 

No there isn't, but you can check out indexOf as a starting point to developing your own, and/or look into regular expressions. It would be a good idea to familiarise yourself with the Javscript string functions.

EDIT: This has been answered before:

Emulating SQL LIKE in JavaScript

karim79
+1  A: 

No.

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

Noon Silk
+2  A: 

You can check the String.match() or the String.indexOf() methods.

Havenard
+1  A: 

No, there isn't any.

The list of comparison operators are listed here.

Comparison Operators

For your requirement the best option would be regular expressions.

rahul
+1  A: 

The closest you could get is using regular expressions. There's tons of examples on the web (such as this one).

fiXedd
A: 

Use the string objects Match method:

// Match a string that ends with abc, similar to LIKE '%abc'
if (theString.match(/^.*abc$/)) 
{ 
    /*Match found */
}

// Match a string that starts with abc, similar to LIKE 'abc%'
if (theString.match(/^abc.*$/)) 
{ 
    /*Match found */
}
Ash