-6

Please explain me the meaning of the following regular expression in JavaScript with proper exploration:

/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/
Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226

1 Answers1

1

This is the meaning.

/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/
1st Alternative: ^\b_((?:__|[\s\S])+?)_\b
^ assert position at start of the string
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
_ matches the character _ literally
1st Capturing group ((?:__|[\s\S])+?)
(?:__|[\s\S])+? Non-capturing group
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
1st Alternative: __
__ matches the characters __ literally
2nd Alternative: [\s\S]
[\s\S] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
\S match any non-white space character [^\r\n\t\f ]
_ matches the character _ literally
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
2nd Alternative: ^\*((?:\*\*|[\s\S])+?)\*(?!\*)
^ assert position at start of the string
\* matches the character * literally
2nd Capturing group ((?:\*\*|[\s\S])+?)
(?:\*\*|[\s\S])+? Non-capturing group
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
1st Alternative: \*\*
\* matches the character * literally
\* matches the character * literally
2nd Alternative: [\s\S]
[\s\S] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
\S match any non-white space character [^\r\n\t\f ]
\* matches the character * literally
(?!\*) Negative Lookahead - Assert that it is impossible to match the regex below
\* matches the character * literally

Well, in a really nice form:

You can check this out at Regex 101.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226
  • 2
    Answering questions with no research effort reassures the poster that these questions are welcome and will continue to do so to possibly no end. I personally do not think this behaviour should be supported. – SeinopSys Jan 06 '16 at 12:21
  • @DJDavid98 I understand. I check the intellect of the OP before answering such questions. FWIW, for this OP, it doesn't matter if I spend 10 mins or lose 2 rep. `:)` Happy to help. – Praveen Kumar Purushothaman Jan 06 '16 at 12:22