The code is showed as follows:
alert(/symbol([.\n]+?)symbol/gi.test('symbolbbbbsymbol'));
or
alert(/#([.\n]+?)#/gi.test('#bbbb#'));
The code is showed as follows:
alert(/symbol([.\n]+?)symbol/gi.test('symbolbbbbsymbol'));
or
alert(/#([.\n]+?)#/gi.test('#bbbb#'));
Because you are looking for dots with a character class inside of <
and >
. Remove the character class:
/<(.+?)>/
First code block should be using this pattern: /symbol(.+?)symbol/
Second code block should be using this pattern: /#(.+?)#/
The regex returns false because a dot loses its special power to match any character (but newlines) when placed within a character class []
- it only matches a simple "."
.
To match and capture the substring delimited at either end by the same single character, the most efficient pattern to use is
/#([^#]+)#/
To match and capture the substring delimited at either end by the same sequence of characters, the pattern to use is
/symbol(.+?)symbol/
or, if you want to match across newlines
/symbol([\s\S]+?)symbol/
where [\s\S]
matches any space or non-space character, which equates to any character.
The ?
is inlcuded to make the pattern match lazily, i.e. to make sure the match ends on the first occurence of "symbol"
.