开发者

What does the look ahead in Regex do?

开发者 https://www.devze.com 2023-01-29 00:50 出处:网络
I need help deciphering Regex (\\*\\*|__)(?=\\S)([^\\r]*?\\S[*_]*)\\1 This came from showdown.js (\\*\\*|__)match ** or __

I need help deciphering Regex

(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1

This came from showdown.js

(\*\*|__)          match ** or __
(?=\S)             -> look ahead for *one* non-space character? what for?开发者_JAVA百科 
([^\r]*?\S[*_]*)   -> zero or more non-carriage-returns, why newlines \n allowed?, one non-space, zero or more * or _ characters
\1                 ends with 1st capture: ** or __

I mainly don't get the 2nd & 3rd lines


I'll take a stab at the second part (keep in mind I guess based on the knowledge that this is a JS Markdown parser):

The lookahead assertion (?=\S) is probably there in case someone wants to write two asterisks ** or two underscores __ without wanting to bold the text that comes after it that is separated by a space (see what I did there?).

** This text will not be bold. **
**This text will be bold.**

** This text will not be bold. **
This text will be bold.

0

精彩评论

暂无评论...
验证码 换一张
取 消