JavaScript operator similar to SQL "like" [duplicate]
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.
No.
You want to use: .indexOf("foo")
and then check the index. If it's >= 0, it contains that string.
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 */
}
You can check the String.match
() or the String.indexOf()
methods.
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 JavaScript string functions.
EDIT: This has been answered before:
Emulating SQL LIKE in JavaScript