-4
[link](url)

上記のパターンを検索して次のコードを返す正規表現を作成しようとしています。

<a href="url">link</a>
4

1 に答える 1

4

チュートリアルを読むことで、私は推測します。

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()

于 2012-11-18T18:33:07.763 に答える