与 MySQL 的 %LIKE% 子句等效的 JavaScript 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7615997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the JavaScript equivalent of MySQL's %LIKE% clause?
提问by TK123
In MySQL I use the like clause:
在 MySQL 中,我使用 like 子句:
LIKE %some keyword%
to perform searches on database records. What can I use to similarly do a search in a JavaScript? So if a user enters something like
对数据库记录执行搜索。我可以用什么来类似地在 JavaScript 中进行搜索?因此,如果用户输入类似
ger
it will show
它会显示
german shepard
Can't use:
不能使用:
str.search('ger');
since it seems to only match whole words
因为它似乎只匹配整个单词
回答by Mohsen
回答by NT3RP
I'm not sure exactly in what context you are using the like operator (e.g. a form field), but you probably want to take advantage of javascript's regular expression object.
我不确定您在什么上下文中使用 like 运算符(例如表单字段),但您可能想要利用 javascript 的正则表达式对象。
A simple example (in your case, for a LIKE
query), might look like this:
一个简单的示例(在您的情况下,用于LIKE
查询)可能如下所示:
var regex = /.*ger.*/
var matchesRegex = regex.test(yourString);
if (matchesRegex) {
//do something
}
Alternatively, you can search for the incidence of your string using the indexOf
operation:
或者,您可以使用以下indexOf
操作搜索字符串的发生率:
var matches = yourString.indexOf("ger") >= 0 ? true : false;
if (matches) {
//do something
}
回答by Billy Moon
search
function does not only return whole words. Maybe you are getting confused by the fact it returns zero-based index, so...
search
函数不仅返回整个单词。也许你对它返回从零开始的索引感到困惑,所以......
// this returns 0 as positive match at position 0
"german shepherd".search('ger')
// this returns -1 as negative match
"german shepherd".search('gerx')
So you need to do comparison of result of search against -1 to see if it is a match or not - you can not just check for true/false.
因此,您需要将搜索结果与 -1 进行比较以查看它是否匹配 - 您不能只检查真/假。
So you could do...
所以你可以这样做...
if(str.search('ger') !== -1){
// matches string
} else {
// does not match string
}
// or another way
// add one to convert result to one-based index, and check truthiness
if(str.search('ger')+1){
// there is a match
} else {
// there is not a match
}
回答by maerics
Assuming you've got a bunch of records as an array of strings you can use the JavaScript built-in Array.filter
method:
假设您有一堆记录作为字符串数组,您可以使用JavaScript 内置Array.filter
方法:
var ss = ['german shepard', 'labrador', 'chihuahua'];
var matches = ss.filter(function(s) {
return s.match(/ger/);
};
matches; // => ['german shepard']
Or if your target JavaScript doesn't implement that method (e.g. it implements ECMAScript prior to 5th edition), then you can do a simple array search, e.g:
或者,如果您的目标 JavaScript 没有实现该方法(例如,它在第 5 版之前实现了 ECMAScript),那么您可以进行简单的数组搜索,例如:
var searchArray = function(arr, regex) {
var matches=[], i;
for (i=0; i<arr.length; i++) {
if (arr[i].match(regex)) matches.push(arr[i]);
}
return matches;
};
searchArray(ss, /h/); // => ['german shepard', 'chihuahua']
回答by Ryan
Here is an easy way to do it in JQuery.
这是在 JQuery 中执行此操作的简单方法。
This uses a filtering of text data inside of a table via a data-filter on the table row, but it can easily be used for other purposes.
这通过表格行上的数据过滤器对表格内的文本数据进行过滤,但它可以很容易地用于其他目的。
$('#search').on('keyup', function() {
var val = $.trim(this.value);
if (val) {
$('tr[data-filter!=' + val + ']').hide();
$('tr[data-filter^=' + val + ']').show();
} else {
$('tr[data-filter]').show();
}
});
In this example it will hide all table rows where an exact match wasn't found, and then filter by the start of the string value. The trim() function is useful in case all that's there is empty spaces, then it will still display everything as if a search was never made. If you want it to display the searched input where it's anywhere on the string, use a * instead of a ^. Both special characters are case sensitive. Also be sure that the hide command displays first and that both are present, or it won't work properly.
在此示例中,它将隐藏未找到完全匹配的所有表行,然后按字符串值的开头进行过滤。如果所有内容都有空格,trim() 函数很有用,那么它仍然会显示所有内容,就好像从未进行过搜索一样。如果您希望它在字符串的任何位置显示搜索到的输入,请使用 * 而不是 ^。两个特殊字符都区分大小写。还要确保首先显示 hide 命令并且两者都存在,否则它将无法正常工作。
See another example here that utilizes both the ^ and * characters: http://jsfiddle.net/heatwaveo8/2VKae/
请参阅此处使用 ^ 和 * 字符的另一个示例:http: //jsfiddle.net/heatwaveo8/2VKae/
回答by Ryan
my code:
我的代码:
// file name 1 = "E_000096_01.jpg"
// file name 2 = "E_000096_02.jpg"
// file name 3 = "E_000096_03.jpg"
// file name 4 = "E_000095_01.jpg"
var code = "000096";
var files = fs.readdirSync(dir).filter(file => file.indexOf(code) >= 0 ? true : false)
// result
console.log(files)
//["E_000096_01.jpg","E_000096_02.jpg","E_000096_03.jpg"]
回答by Erik
I'm not sure if all browsers support all of these methods: http://www.javascriptkit.com/jsref/string.shtml
我不确定是否所有浏览器都支持所有这些方法:http: //www.javascriptkit.com/jsref/string.shtml
But it looks like there's a String.match()
method that'll do what you want.
但看起来有一种String.match()
方法可以满足您的需求。