是否有允许使用正则表达式的 JavaScript 的 String.indexOf() 版本? [英] Is there a version of JavaScript's String.indexOf() that allows for regular expressions?
问题描述
在 javascript 中,是否有 String.indexOf() 的等价物,它采用正则表达式而不是第一个参数的字符串,同时仍然允许第二个参数?
In javascript, is there an equivalent of String.indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ?
我需要做类似的事情
str.indexOf(/[abc]/ , i);
和
str.lastIndexOf(/[abc]/ , i);
虽然 String.search() 将正则表达式作为参数,但它不允许我指定第二个参数!
While String.search() takes a regexp as a parameter it does not allow me to specify a second argument!
结果证明这比我原先想象的要难,所以我编写了一个小的测试函数来测试所有提供的解决方案......它假设 regexIndexOf 和 regexLastIndexOf 已被添加到 String 对象中.
This turned out to be harder than I originally thought so I wrote a small test function to test all the provided solutions... it assumes regexIndexOf and regexLastIndexOf have been added to the String object.
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)]) ;
}
}
我正在测试如下以确保至少对于一个字符的正则表达式,结果与我们使用 indexOf 的结果相同
and I am testing as follow to make sure that at least for one character regexp, the result is the same as if we used indexOf
//在xes中寻找a
测试('xxx');
测试('axx');
测试('xax');
测试('xxa');
测试('axa');
测试('xaa');
测试('aax');
测试('aaa');
//Look for the a among the xes
test('xxx');
test('axx');
test('xax');
test('xxa');
test('axa');
test('xaa');
test('aax');
test('aaa');
推荐答案
结合一些已经提到的方法(indexOf 显然相当简单),我认为这些是可以解决问题的函数:
Combining a few of the approaches already mentioned (the indexOf is obviously rather simple), I think these are the functions that will do the trick:
function regexIndexOf(string, regex, startpos) {
var indexOf = string.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
function regexLastIndexOf(string, regex, startpos) {
regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
if(typeof (startpos) == "undefined") {
startpos = string.length;
} else if(startpos < 0) {
startpos = 0;
}
var stringToWorkWith = string.substring(0, startpos + 1);
var lastIndexOf = -1;
var nextStop = 0;
while((result = regex.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
regex.lastIndex = ++nextStop;
}
return lastIndexOf;
}
更新:编辑了 regexLastIndexOf()
以便现在似乎模仿 lastIndexOf()
.请让我知道它是否仍然失败以及在什么情况下.
UPDATE: Edited regexLastIndexOf()
so that is seems to mimic lastIndexOf()
now. Please let me know if it still fails and under what circumstances.
更新:通过本页评论中发现的所有测试,以及我自己的测试.当然,这并不意味着它是防弹的.任何反馈表示赞赏.
UPDATE: Passes all tests found on in comments on this page, and my own. Of course, that doesn't mean it's bulletproof. Any feedback appreciated.
这篇关于是否有允许使用正则表达式的 JavaScript 的 String.indexOf() 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!