-1

The code is showed as follows:

alert(/symbol([.\n]+?)symbol/gi.test('symbolbbbbsymbol'));

or

alert(/#([.\n]+?)#/gi.test('#bbbb#'));
fancy
  • 71
  • 7
  • 1
    Change to `/<(.+?)>/gi` . Also, don't parse HTML with RegExp that's a horrible idea http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Benjamin Gruenbaum Mar 10 '13 at 16:41
  • Why did you change the code in the question? – gdoron Mar 10 '13 at 16:44
  • I am sorry for the incomplete code posted before.What I want is to find all strings between two symbols such as '#***#' or '***' – fancy Mar 10 '13 at 16:49
  • The principle is the same: `[.]` matches literally the `.` character and nothing else. – JJJ Mar 10 '13 at 16:52

2 Answers2

8

Because you are looking for dots with a character class inside of < and >. Remove the character class:

/<(.+?)>/

Clarification after question edit:

First code block should be using this pattern: /symbol(.+?)symbol/

Second code block should be using this pattern: /#(.+?)#/

Daedalus
  • 1,667
  • 10
  • 12
2

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".

MikeM
  • 13,156
  • 2
  • 34
  • 47