JavaScript operator similar to SQL "like"
JavascriptJavascript Problem Overview
> 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.
Javascript Solutions
Solution 1 - Javascript
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.
Solution 2 - Javascript
No.
You want to use: .indexOf("foo")
and then check the index. If it's >= 0, it contains that string.
Solution 3 - Javascript
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 */
}
Solution 4 - Javascript
You can check the http://www.w3schools.com/jsref/jsref_match.asp">`String.match`()</a> or the http://www.w3schools.com/jsref/jsref_indexOf.asp">`String.indexOf()`</a> methods.
Solution 5 - Javascript
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:
Solution 6 - Javascript
No, there isn't any.
The list of comparison operators are listed here.
For your requirement the best option would be regular expressions.