在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”);


当前回答

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

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

其他回答

我使用string .prototype.match(regex),它返回一个字符串数组,所有找到的匹配给定的正则表达式在字符串(更多信息见这里):

function getLastIndex(text, regex, limit = text.length) { const matches = text.match(regex); // no matches found if (!matches) { return -1; } // matches found but first index greater than limit if (text.indexOf(matches[0] + matches[0].length) > limit) { return -1; } // reduce index until smaller than limit let i = matches.length - 1; let index = text.lastIndexOf(matches[i]); while (index > limit && i >= 0) { i--; index = text.lastIndexOf(matches[i]); } return index > limit ? -1 : index; } // expect -1 as first index === 14 console.log(getLastIndex('First Sentence. Last Sentence. Unfinished', /\. /g, 10)); // expect 29 console.log(getLastIndex('First Sentence. Last Sentence. Unfinished', /\. /g));

杰森·邦廷的最后一个指数不成立。我的方法不是最优的,但有效。

//Jason Bunting's
String.prototype.regexIndexOf = function(regex, startpos) {
var indexOf = this.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}

String.prototype.regexLastIndexOf = function(regex, startpos) {
var lastIndex = -1;
var index = this.regexIndexOf( regex );
startpos = startpos === undefined ? this.length : startpos;

while ( index >= 0 && index < startpos )
{
    lastIndex = index;
    index = this.regexIndexOf( regex, index + 1 );
}
return lastIndex;
}

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

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

它不是原生的,但您当然可以添加此功能

<script type="text/javascript">

String.prototype.regexIndexOf = function( pattern, startIndex )
{
    startIndex = startIndex || 0;
    var searchResult = this.substr( startIndex ).search( pattern );
    return ( -1 === searchResult ) ? -1 : searchResult + startIndex;
}

String.prototype.regexLastIndexOf = function( pattern, startIndex )
{
    startIndex = startIndex === undefined ? this.length : startIndex;
    var searchResult = this.substr( 0, startIndex ).reverse().regexIndexOf( pattern, 0 );
    return ( -1 === searchResult ) ? -1 : this.length - ++searchResult;
}

String.prototype.reverse = function()
{
    return this.split('').reverse().join('');
}

// Indexes 0123456789
var str = 'caabbccdda';

alert( [
        str.regexIndexOf( /[cd]/, 4 )
    ,   str.regexLastIndexOf( /[cd]/, 4 )
    ,   str.regexIndexOf( /[yz]/, 4 )
    ,   str.regexLastIndexOf( /[yz]/, 4 )
    ,   str.lastIndexOf( 'd', 4 )
    ,   str.regexLastIndexOf( /d/, 4 )
    ,   str.lastIndexOf( 'd' )
    ,   str.regexLastIndexOf( /d/ )
    ]
);

</script>

我没有完全测试这些方法,但到目前为止,它们似乎是有效的。

如果您正在使用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;
};