-3
A
回答
4
By reading a tutorial, I guess.
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
然后我们引用的两个摄像组,$1
和$2
替换字符串。 $1
正在捕获[]
中的字符和$2
正在捕获()
中的字符。
+1
我真的希望人们不会鼓励这类问题。 –
+0
@ user1689607我明白了你的观点,但我相信3分钟的降薪和4分钟内的封闭问题令人沮丧。而且我觉得只有教程链接会是一个狡猾的答案。 –
+0
是的教程链接本身就是一个不好的答案。我当然不批评你的答案的内容......只是它的存在。 ;-) –
相关问题
- 1. Javascript正则表达式检查模式
- 2. Javascript正则表达式模式“{{text}}”
- 3. Javascript多个正则表达式模式
- 4. Javascript正则表达式模式
- 5. 正则表达式javascript模式
- 6. Javascript正则表达式寻找模式
- 7. javascript模式的正则表达式
- 8. 正则表达式 - 模式
- 9. 正则表达式模式
- 10. 正则表达式模式
- 11. 正则表达式模式
- 12. 正则表达式模式
- 13. 正则表达式模式
- 14. 正则表达式正则表达式模仿正则表达式
- 15. Python的正则表达式:“喜欢”正则表达式模式?
- 16. 正则表达式(正则表达式)模式匹配
- 17. 设置从正则表达式模式到正则表达式?
- 18. 正则表达式模式的正则表达式模式是什么?
- 19. JavaScript正则表达式模糊
- 20. 正则表达式(正则表达式)
- 21. 正则表达式(正则表达式)
- 22. 正则表达式(正则表达式)
- 23. 正则表达式在javascript - 正则表达式中的变量
- 24. Javascript lastIndex正则表达式属性到PHP正则表达式
- 25. Firefox正则表达式中的Javascript正则表达式
- 26. 正则表达式(正则表达式):/ W连字符(javascript)
- 27. JavaScript中的正则表达式(正则表达式)
- 28. PHP + JAVASCRIPT GET正则表达式的URL正则表达式
- 29. (教学)的JavaScript - 使用正则表达式(正则表达式)
- 30. 正则表达式的JavaScript正则表达式
您将首先从信誉良好的来源学习正则表达式的基础知识,如[MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions)。 –
也许使用一些已经存在的解决方案,如Markdown; [pagedown](http://code.google.com/p/pagedown/)是一个JavaScript Markdown转换器和编辑器,与此处在StackOverflow中使用的类似。 – Gumbo