JavaScript operator similar to SQL "like"

Javascript

Javascript 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

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:

Emulating SQL LIKE in JavaScript

Solution 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioninduView Question on Stackoverflow
Solution 1 - JavascriptcletusView Answer on Stackoverflow
Solution 2 - JavascriptNoon SilkView Answer on Stackoverflow
Solution 3 - JavascriptAshView Answer on Stackoverflow
Solution 4 - JavascriptHavenardView Answer on Stackoverflow
Solution 5 - Javascriptkarim79View Answer on Stackoverflow
Solution 6 - JavascriptrahulView Answer on Stackoverflow