Javascript 在字符串中搜索包含多个字符的字符串

Javascript 在字符串中搜索包含多个字符的字符串,javascript,angular,string,indexof,Javascript,Angular,String,Indexof,我有两个字符串类似于“L7-LO”和“%L7-LO” 如果String只包含“--”,我需要一些基于此的处理,如果字符串包含这两个字符“-”,“%”,我只需要考虑%而忽略“--”字符。 为了这个目的,我在下面做了这个 if (this.selectedSources[formula].Value.indexOf('%') == -1) { this.formulaType = "percent" } else if (this.selectedSources[formula

我有两个字符串类似于“L7-LO”和“%L7-LO”

如果String只包含“--”,我需要一些基于此的处理,如果字符串包含这两个字符“-”,“%”,我只需要考虑%而忽略“--”字符。 为了这个目的,我在下面做了这个

   if (this.selectedSources[formula].Value.indexOf('%') == -1) {
    this.formulaType = "percent"
  }
  else if (this.selectedSources[formula].Value.indexOf('-') == -1) {
    this.formulaType = "diff";
  }
但有些人认为上述代码不起作用


如果条件需要更改,如果角度中有两个字符,请告诉我如何仅区分一个字符。其余的看起来不错-

if (this.selectedSources[formula].Value.indexOf('%') !== -1) {
    this.formulaType = "percent"
 }
  else if (this.selectedSources[formula].Value.indexOf('-') !== -1) {
    this.formulaType = "diff";
 }

如果条件需要改变。其余的看起来不错-

if (this.selectedSources[formula].Value.indexOf('%') !== -1) {
    this.formulaType = "percent"
 }
  else if (this.selectedSources[formula].Value.indexOf('-') !== -1) {
    this.formulaType = "diff";
 }

如果您需要测试字符串中的
%
-
,我会使用正则表达式:

const regex = new RegEx(/.*[%]{1}.*[-]{1}/);
this.formulaType = regex.test(this.selectedSources[formula].Value) ? 'percent' : 'diff';
否则,您可以只使用
.indexOf('%')


如果您需要测试字符串中的
%
-
,我会使用正则表达式:

const regex = new RegEx(/.*[%]{1}.*[-]{1}/);
this.formulaType = regex.test(this.selectedSources[formula].Value) ? 'percent' : 'diff';
否则,您可以只使用
.indexOf('%')


你的逻辑颠倒了吗?你基本上说的是“如果不是“%”,那么是百分比,如果不是“-”,那么是差异。我希望测试是
if(…indexOf(“%”)>=0{this.formulaType=“percent”;}
你的逻辑颠倒了吗?你基本上说的是“如果不是“%”,那么是百分比,如果不是“-”,那么是差异。我希望测试是
if(…indexOf(“%”)>=0{this.formulaType=“percent”;}