是否有允许正则表达式的JavaScript的String.indexOf()版本? [英] Is there a version of JavaScript's String.indexOf() that allows for regular expressions?

查看:175
本文介绍了是否有允许正则表达式的JavaScript的String.indexOf()版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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()将regexp作为参数,但它不允许我指定秒参数!

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)]) ;
    }
}

我正在测试如下以确保在至少对于一个字符regexp,结果与我们使用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 ...
test(' xxx');

test('axx');

test('xax');

test('xxa');

test('axa');

test('xaa');

test('aax');

test('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:

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) {
    regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
    if(typeof (startpos) == "undefined") {
        startpos = this.length;
    } else if(startpos < 0) {
        startpos = 0;
    }
    var stringToWorkWith = this.substring(0, startpos + 1);
    var lastIndexOf = -1;
    var nextStop = 0;
    while((result = regex.exec(stringToWorkWith)) != null) {
        lastIndexOf = result.index;
        regex.lastIndex = ++nextStop;
    }
    return lastIndexOf;
}

显然,修改内置的String对象会为大多数人发送红色标记人们,但这可能是一次没有那么大的交易;只需要注意它。

Obviously, modifying the built-in String object would send up red flags for most people, but this may be one time when it is not that big of a deal; simply be aware of it.

更新:编辑 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆