在javascript中,是否有一个string . indexof()的等效,为第一个参数接受正则表达式而不是字符串,同时仍然允许第二个参数?

我需要做点什么

str.indexOf(/[abc]/ , i);

and

str.lastIndexOf(/[abc]/ , i);

虽然String.search()接受regexp作为参数,但它不允许我指定第二个参数!

编辑: 这比我最初想象的要难,所以我写了一个小测试函数来测试所有提供的解决方案……它假设regexIndexOf和regexLastIndexOf已经添加到String对象中。

function test (str) {
    var i = str.length +2;
    while (i--) {
        if (str.indexOf('a',i) != str.regexIndexOf(/a/,i)) 
            alert (['failed regexIndexOf ' , str,i , str.indexOf('a',i) , str.regexIndexOf(/a/,i)]) ;
        if (str.lastIndexOf('a',i) != str.regexLastIndexOf(/a/,i) ) 
            alert (['failed regexLastIndexOf ' , str,i,str.lastIndexOf('a',i) , str.regexLastIndexOf(/a/,i)]) ;
    }
}

我正在进行如下测试,以确保至少对于一个字符regexp,如果我们使用indexOf,结果是相同的

在 xes 中寻找 a 测试(“xxx”); 测试('axx'); 测试(“xax”); 测试(“XXA”); 测试(“AXA”); 测试(“xaa”); 测试(“AAX”); 测试(“AAA”);


当前回答

如果您正在使用RegExp寻找一个非常简单的lastIndex查找,并且不关心它是否完全模仿了lastIndexOf,那么这可能会引起您的注意。

我只是将字符串反向,并从length - 1中减去第一个出现索引。它碰巧通过了我的测试,但我认为长字符串可能会出现性能问题。

interface String {
  reverse(): string;
  lastIndex(regex: RegExp): number;
}

String.prototype.reverse = function(this: string) {
  return this.split("")
    .reverse()
    .join("");
};

String.prototype.lastIndex = function(this: string, regex: RegExp) {
  const exec = regex.exec(this.reverse());
  return exec === null ? -1 : this.length - 1 - exec.index;
};

其他回答

String构造函数的实例有一个.search()方法,该方法接受RegExp并返回第一个匹配项的索引。

要从特定位置开始搜索(伪造.indexOf()的第二个参数),可以切掉前i个字符:

str.slice(i).search(/re/)

但这将获得较短字符串的索引(在第一部分被切掉之后),因此如果返回的索引不是-1,则需要将切掉的部分(i)的长度添加到返回的索引中。这将给你原始字符串的索引:

function regexIndexOf(text, re, i) {
    var indexInSuffix = text.slice(i).search(re);
    return indexInSuffix < 0 ? indexInSuffix : indexInSuffix + i;
}

来自Jason Bunting的regexIndexOf可以更简单地反向,并且仍然支持UTF8字符:

function regexLastIndexOf(string, regex, startpos=0) {
    return text.length - regexIndexOf([...text].reverse().join(""), regex, startpos) - 1;
}

对于具有稀疏匹配的数据,使用字符串。跨浏览器搜索速度最快。它每次迭代都会重新切片字符串:

function lastIndexOfSearch(string, regex, index) {
  if(index === 0 || index)
     string = string.slice(0, Math.max(0,index));
  var idx;
  var offset = -1;
  while ((idx = string.search(regex)) !== -1) {
    offset += idx + 1;
    string = string.slice(idx + 1);
  }
  return offset;
}

对于密集的数据,我做了这个。与执行方法相比,它比较复杂,但对于密集数据,它比我尝试过的其他方法快2-10倍,比公认的解决方案快100倍左右。要点如下:

It calls exec on the regex passed in once to verify there is a match or quit early. I do this using (?= in a similar method, but on IE checking with exec is dramatically faster. It constructs and caches a modified regex in the format '(r).(?!.?r)' The new regex is executed and the results from either that exec, or the first exec, are returned; function lastIndexOfGroupSimple(string, regex, index) { if (index === 0 || index) string = string.slice(0, Math.max(0, index + 1)); regex.lastIndex = 0; var lastRegex, index flags = 'g' + (regex.multiline ? 'm' : '') + (regex.ignoreCase ? 'i' : ''), key = regex.source + '$' + flags, match = regex.exec(string); if (!match) return -1; if (lastIndexOfGroupSimple.cache === undefined) lastIndexOfGroupSimple.cache = {}; lastRegex = lastIndexOfGroupSimple.cache[key]; if (!lastRegex) lastIndexOfGroupSimple.cache[key] = lastRegex = new RegExp('.*(' + regex.source + ')(?!.*?' + regex.source + ')', flags); index = match.index; lastRegex.lastIndex = match.index; return (match = lastRegex.exec(string)) ? lastRegex.lastIndex - match[1].length : index; };

方法的jsPerf

我不明白上面这些测试的目的。需要正则表达式的情况是不可能与调用indexOf进行比较的,我认为这是首先创建该方法的目的。为了让测试通过,使用'xxx+(?!x)'比调整regex迭代的方式更有意义。

如果您正在使用RegExp寻找一个非常简单的lastIndex查找,并且不关心它是否完全模仿了lastIndexOf,那么这可能会引起您的注意。

我只是将字符串反向,并从length - 1中减去第一个出现索引。它碰巧通过了我的测试,但我认为长字符串可能会出现性能问题。

interface String {
  reverse(): string;
  lastIndex(regex: RegExp): number;
}

String.prototype.reverse = function(this: string) {
  return this.split("")
    .reverse()
    .join("");
};

String.prototype.lastIndex = function(this: string, regex: RegExp) {
  const exec = regex.exec(this.reverse());
  return exec === null ? -1 : this.length - 1 - exec.index;
};

Use:

str.search(regex)

请在这里查看文档。