我正在尝试创建一个正则表达式,它将在字符串中的任何位置找到字符。不过,我希望他们首先找到连续的字符。
让我举个例子,假设s = 'this is a test test string'
我正在寻找tst
我想像这样找到它:
// Correct
// v vv
s = 'this is a test test string'
并不是:
// Incorrect
// v v v
s = 'this is a test test string'
还有如果s = 'this is a test test tst string'
// Correct
// vvv
s = 'this is a test test tst string'
有几点需要注意:
- 搜索字符是用户提供的(
tst
在这种情况下) - 我正在使用 javascript,所以我不能支持 atomi 分组,我怀疑这会让这更容易
我最好的尝试是这样的:
var find = 'tst';
var rStarts = [];
var rEnds = [];
for (var i = 0; i < find.length - 1; i++) {
rStarts.push(= '(' + find[i] + find[i + 1] )
rEnds.push( find[i] + '[^]*?' + find[i + 1] + ')' );
}
但是在进行到一半时,我意识到我不知道我要去哪里。任何想法如何做到这一点?