如何将 RegExp 放入带有 javascript 变量的 JavaScript 对象字面量 aka 对象初始值设定项
How to put a RegExp inside a JavaScript object literal aka object initializer with a javascript variable
我希望过滤的目的是将正则表达式放在 javascript 模板中的 javascript 变量上,这里是过滤器(没有正则表达式):
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: sValue
});
这对应于一个组合框(覆盖搜索),此过滤器适用于查找但不适用于包含,仅适用于与行开头的精确匹配,然后我想使用 RegEx 然后我尝试了:
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: /sValue/i
});
它不起作用(我想将 sValue 视为 'sValue')然后我尝试了:
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: new RegExp(sValue, "i")
});
它也没有用(我想是因为格式?)。
那么我怎么能做类似的事情呢?
提前谢谢你:)
PS:为了同样的目的,我也试过另一个分类:
sValue = oEvent.target.value,
oItem=this.getItems()
aVisibleItems1 = this.setFilterFunction(function(sValue, oItem) {
return this.oItem.getText().match(new RegExp(sValue, "i"))
});
和
sValue = oEvent.target.value,
aVisibleItems1 = this.setFilterFunction(searchTextContain(this.getItems(), sValue));
function searchTextContain(items, sValueToSearch) {
var sResearch = [];
for (var item of items) {
if (item.getText().match(new RegExp(sValueToSearch, "i")) != null) {
sResearch.push(item);
}
}
return sResearch;
}
它也没有用,我更喜欢一个看起来像 2 的解决方案,如果有的话,首先尝试使用 RegExp,因为我必须在 UI 中使用 this.filterItems 作为过滤器listItem,但如果您有其他解决方案,我也会很高兴,目的是过滤值
我找到了一个解决方案,因为我的问题是 filterItems 我覆盖了这个函数,
我创建了一个控件 ComboBoxContain,它覆盖了 ComboBox filterItems,如下所示:
ComboBox.prototype.filterItems = function(mOptions, aItems) {
var sProperty = mOptions.property,
sValue = mOptions.value,
bEmptyValue = sValue === "",
bMatch = false,
sMutator = "get" + sProperty.charAt(0).toUpperCase() + sProperty.slice(1),
aFilteredItems = [],
oItem = null;
aItems = aItems || this.getItems();
if (!bEmptyValue) {
for (var i = 0; i < aItems.length; i++) {
oItem = aItems[i];
// the item match with the value
bMatch = (oItem[sMutator]().match(new RegExp(sValue, "i")) !== null);
if (bMatch) {
aFilteredItems.push(oItem);
}
this._setItemVisibility(oItem, bMatch);
}
}
return aFilteredItems;
};
我希望过滤的目的是将正则表达式放在 javascript 模板中的 javascript 变量上,这里是过滤器(没有正则表达式):
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: sValue
});
这对应于一个组合框(覆盖搜索),此过滤器适用于查找但不适用于包含,仅适用于与行开头的精确匹配,然后我想使用 RegEx 然后我尝试了:
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: /sValue/i
});
它不起作用(我想将 sValue 视为 'sValue')然后我尝试了:
sValue = oEvent.target.value,
aVisibleItems1 = this.filterItems({
property: "text",
value: new RegExp(sValue, "i")
});
它也没有用(我想是因为格式?)。
那么我怎么能做类似的事情呢?
提前谢谢你:)
PS:为了同样的目的,我也试过另一个分类:
sValue = oEvent.target.value,
oItem=this.getItems()
aVisibleItems1 = this.setFilterFunction(function(sValue, oItem) {
return this.oItem.getText().match(new RegExp(sValue, "i"))
});
和
sValue = oEvent.target.value,
aVisibleItems1 = this.setFilterFunction(searchTextContain(this.getItems(), sValue));
function searchTextContain(items, sValueToSearch) {
var sResearch = [];
for (var item of items) {
if (item.getText().match(new RegExp(sValueToSearch, "i")) != null) {
sResearch.push(item);
}
}
return sResearch;
}
它也没有用,我更喜欢一个看起来像 2 的解决方案,如果有的话,首先尝试使用 RegExp,因为我必须在 UI 中使用 this.filterItems 作为过滤器listItem,但如果您有其他解决方案,我也会很高兴,目的是过滤值
我找到了一个解决方案,因为我的问题是 filterItems 我覆盖了这个函数, 我创建了一个控件 ComboBoxContain,它覆盖了 ComboBox filterItems,如下所示:
ComboBox.prototype.filterItems = function(mOptions, aItems) {
var sProperty = mOptions.property,
sValue = mOptions.value,
bEmptyValue = sValue === "",
bMatch = false,
sMutator = "get" + sProperty.charAt(0).toUpperCase() + sProperty.slice(1),
aFilteredItems = [],
oItem = null;
aItems = aItems || this.getItems();
if (!bEmptyValue) {
for (var i = 0; i < aItems.length; i++) {
oItem = aItems[i];
// the item match with the value
bMatch = (oItem[sMutator]().match(new RegExp(sValue, "i")) !== null);
if (bMatch) {
aFilteredItems.push(oItem);
}
this._setItemVisibility(oItem, bMatch);
}
}
return aFilteredItems;
};