搜索时如何仅使用星号通配符? [英] How to use just asterisk wildcard when searching?
问题描述
我只想支持星号(*)通配符而不是正则表达式.
I want to just support asterisk(*) wildcard not regex.
这是我的代码:
for example : checkNames[i] = "Number of ongoing MS sessions"
var checkName = checkNames[i].split("*").join(".*");
supportRegExp(dataSource[j].ColumnName, checkName, validatedList,dataSource[j]);
这是我的 supportRegExp 函数:
and this is my supportRegExp function :
function supportRegExp(arrayElem, checkName, validatedList, elem) {
var regexpArr = checkName
.replace(/\t/g, '\n')
.split("\n")
.map(function (item) {
return "^" + item + "$";
});
var regexp = new RegExp(regexpArr, 'i');
$my.isMatched = regexp.test(arrayElem)
if ($my.isMatched) {
validatedList.push(elem); //elem: my object { inside ColumnName, DisplayName }
AddValidatedList(arrayElem); //arrayElem : elem.ColumnName or elem.DisplayName bla bla.
}
}
这是有效的.我正在写num*"和即将到来的结果,然后我正在写num|m"和即将到来的结果.因为我使用的是正则表达式,所以我只想支持 '*' 符号.
This is works. I am writing "num*" and coming result and then I am writing "num|m" and coming result. Because I am using regexp so I want to just support '*' sign.
例如:当我写 num* 时,结果应该出现,但我写的 num|m 不应该出现结果.因为我只想支持星号.
For example : When I am writing num*, result should come but I am writing num|m should not come result. Because I want to just support asterisk sign.
我该怎么办?
请有任何想法.
推荐答案
大量借用 这个问题,我认为您在追求以下内容:
Borrowing heavily from this question, I think you are after something like the following:
var checkName =
checkNames[i]
.split("*")
.map(function (s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); })
.join(".*");
在分割 *
字符后,这会转义正则表达式的每个部分,准备与 .*
连接起来.
This escapes each part of the regular expression after splitting on the *
character, ready to be joined back with .*
.
这篇关于搜索时如何仅使用星号通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!