Here is the code :
> var reg = new RegExp(" hel.lo ", 'g');
>
> var str = " helalo helblo helclo heldlo ";
>
> var mat = str.match(reg);
>
> alert(mat);
It alerts “helalo, helclo”, but i expect it to be “helalo, helblo, helclo, heldlo” .
Only the half of them matches, I guess that’s because of the space wich count only once. So I tried to double every space before processing, but in some case it’s not enough.
I’m looking for an explanation, and a solution.
Thx
When
␣helalo␣was matched, the string left ishelblo␣...without the leading space. But the regex requires a leading space, so it skips to␣helclo␣.To avoid the expression eating up the space, use a lookahead.
(Or use
\bas a word boundary.)