Roger's solution is clearly better than what i'm about to suggest, but you can also limit the positions with your regex.. in this case something like /^.{10,20}hello world/si since, for 'hello world' to be in the first 30 characters it must not have more than 20 chars (i might have counted wrong by one) preceeding it, but there must be at least 10 before it.