If语句出现字符串问题

If statement with string issues

本文关键字:字符串 问题 语句 If      更新时间:2023-11-16

这是我的代码(我正在编辑Xsplit中用于解析html文件的脚本,并且我正在尝试将最终变量-responseEdited设置为"No active match"。如果变量-resposeEdited包含字符串"winning"。但是,下面代码的结果在该变量中似乎为null。

/*replace some text in the string*/
var responseEdited = responseCleaned.replace('</h3><h3>', 'vs. ');
/*this is the problem area*/
/*if it contains "winning", change to no active match*/
if(responseEdited.contains("winning"))
   {
     responseEdited = "No active match.";
   }

想法?谢谢

要使代码在不更改的情况下工作,只需在脚本顶部添加以下polyfill即可。如果该函数存在于特定浏览器的JavaScript引擎中,那么这将被忽略,否则该函数将被添加到String的原型中。

String.prototype.contains()

if (!String.prototype.includes) {
  String.prototype.includes = function() {'use strict';
    return String.prototype.indexOf.apply(this, arguments) !== -1;
  };
}

这是MDN建议的polyfill。