[link](url)
上記のパターンを検索して次のコードを返す正規表現を作成しようとしています。
<a href="url">link</a>
str = str.replace(/\[([^\]]*)\]\(([^)]*)\)/g, '<a href="$2">$1</a>');
これは少し難しいように見えますが、認めます。説明は次のとおりです。
/ # just the delimiter for the regex (like " for a string)
\[ # match a literal [
( # start capturing group $1 for later access
[^\]] # match any character except ]
* # 0 or more of those (as many as possible)
) # end of capturing group $1
\] # match a literal ]
\( # match a literal (
( # start capturing group $2 for later access
[^)] # match any character except )
* # 0 or more of those (as many as possible)
) # end of capturing group $2
\) # match a literal )
/ # end of regex
g # make regex global to replace ALL occurrences
次に、取り込まれた 2 つのグループを置換文字列内の$1
とで参照します。は 内で文字をキャプチャしており、内で文字をキャプチャしています。$2
$1
[]
$2
()