I have a fairly large string and would like to check if a snippet of it matches a regex. The problem is that this snippet is in the middle of the string, in a very specific position. As I will have to do this several times (different strings, different regexes and different positions) I would like this check to be done efficiently without having to:
Make a substring, which would create new strings unnecessarily:
var sub = str.substring(pos); // Cria um novo objeto, potencialmente bem grande
regex.exec(sub);
or:
Do the search globally, which not only traverses parts of the string that do not interest me (ie those before the desired position) but also may not give me the result I want at all (eg if there is an intersection between a marriage and the part of the string that interests me):
var resultado = regex.exec(str); // Assumindo que regex possui a flag g
while ( resultado && resultado.index < pos )
resultado = regex.exec(str);
if ( resultado.index == pos )
...
Is it possible to do this? The usual wedding methods ( String.match
, RegExp.test
and RegExp.exec
) do not have parameters to specify the position of the string from which to start execution, and even String.search
does not have this option. Is there any other way?