정규 표현식:가격 인하 링크를 HTML 앵커로 변환

일반적인 가격 인하 링크는 괄호/괄호 형식으로 구성됩니다.
[I'm an inline-style link](https://www.google.com)
...하지만 HTML 형식으로 변환할 수 있습니다.
<a href="https://www.google.com">I'm an inline-style link</a>
정규 표현식 변환을 사용하려면 표현식을 사용합니다.
/\[([^\]]+)\]\(([^\)]+)\)/

Javascript의 경우(try it:
var markdown = "[I'm an inline-style link](https://www.google.com)";
var html = markdown.replace(/\[([^\]]+)\]\(([^\)]+)\)/, '<a href="$2">$1</a>');
alert(html);

PHP의 경우try it:
<?php
$markdown = "[I'm an inline-style link](https://www.google.com)";
$html = preg_replace('/\[([^\]]+)\]\(([^\)]+)\)/', '<a href="\2">\1</a>', $markdown);
echo $html;

붕괴
/  \[([^\]]+)\]\(([^\)]+)\)  /

\[([^\]]+)\]
   \[    Look for a literal left bracket, by escaping it
   (     Start a capture group to retrieve the contents
  [^\]]+ Repeatedly find a character that isn't a closing bracket
   )     Close the capture group
   \]    Look for a literal right bracket, by escaping it

\(([^\)]+)\)
   \(    Look for a literal left parenthesis, by escaping it
   (     Start a capture group to retrieve the contents
  [^\)]+ Repeatedly find something that isn't a right parenthesis
   )     Close the capture group
   \)    Look for a literal right parenthesis, by escaping it

좋은 웹페이지 즐겨찾기