如何通过不确切的文本JS过滤

发布于 2025-02-10 12:42:03 字数 719 浏览 1 评论 0原文

这是Ag-Grid JS代码的一部分,该代码具有准确的文本过滤单元格:

function doesExternalFilterPass(node) {
  switch (status) {
    case 'released':
      return node.data.Status == 'Released';
    case 'early-access':
      return node.data.Status == 'Early access';
    case 'in-development':
      return node.data.Status == 'In development';
    case 'rtt-strategy':
      return node.data.Genre == /.'RTT'./;
    default:
      return true;
  }
}

例如,其中一个字符串过滤了所有包含在状态列中的发行的单元,而其他包含 rtt 的单元在流派柱中。 ,但是我需要像 rtt,rts,action 这样的字符串也要过滤。据我了解,我应该使用正则表达式,我尝试此操作,

return node.data.Genre == /.'RTT'./;

但它不起作用。

有什么想法可以进行多个字的过滤?

Here is a part of ag-grid JS code that filter cells with exactly text:

function doesExternalFilterPass(node) {
  switch (status) {
    case 'released':
      return node.data.Status == 'Released';
    case 'early-access':
      return node.data.Status == 'Early access';
    case 'in-development':
      return node.data.Status == 'In development';
    case 'rtt-strategy':
      return node.data.Genre == /.'RTT'./;
    default:
      return true;
  }
}

For example, one of the strings filter all cells containing Released in Status column, other containing RTT in Genre column. But I need that strings like RTT, RTS, Action also to be filtered. As I understand, I should use regular expressions, I try this

return node.data.Genre == /.'RTT'./;

but it doesn't work.

Any ideas to get many-words filtering?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2025-02-17 12:42:03
  1. 您不需要正则是这样。如果可能的话,应避免正则。如果我正确理解,您要检查字符串是否包含子字符串。为此,您可以使用string.prototype.includes()方法: https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/global_objects/string/includes
    示例代码:
case 'rtt-strategy':
  return node.data.Genre.includes('RTT');
  1. 如果您仍然要使用Regex,则可以使用Regexp.prototype.test()方法: https://develoverer.mozilla.org/en-us/docs/web/web/javas/javascript/javascript/reference/reference/global_obal_obableds/global_obignts/regex/regexplestestest < /a>。首先,您确实应该查找如何在JS中使用Regex,因为您的示例代码已经遥遥无期。其次,示例代码:
/RTT/.test(node.data.Genre)

或者如果您要病例不敏感,则:

/RTT/i.test(node.data.Genre)
  1. You don't need regex for this. Regex should be avoided, if possible. If I understand it correctly, you want to check if a string contains a substring. For that you can use String.prototype.includes() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
    Example code:
case 'rtt-strategy':
  return node.data.Genre.includes('RTT');
  1. If you still want to use regex, then you can use the RegExp.prototype.test() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test. First of all, you really should look up how to use regex in js, because your example code is way off. Secondly, example code:
/RTT/.test(node.data.Genre)

Or if you want case insensitive, then:

/RTT/i.test(node.data.Genre)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文