JS Regex -替换markdown链接的内容
JS Regex - replace contents of markdown link
我几乎找到了一个正则表达式问题,只是一件小事。
我正在尝试得到这个:
and so use [chalk](#api).red(string[, options])
这:
and so use chalk.red(string[, options])
我有这个:
var md = 'and so use chalk.red(string[, options])';
console.log(md.replace(/('[.*?']'()(.+?)('))/g, '$1'))
与[x](y)
完全匹配。但是,$1
返回[chalk](
。我希望它返回chalk
而不是,我对如何做到这一点感到困惑。
I might(?) have figure out:
这在所有情况下都有效吗?
/('[(.*?)']'()(.+?)('))/g
让我们看看你当前的正则表达式是做什么的
/('[(.*?)']'()(.+?)('))/g
1st Capturing group ('[(.*?)']'()
'[ matches the character [ literally
2nd Capturing group (.*?)
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
'] matches the character ] literally
'( matches the character ( literally
3rd Capturing group (.+?)
.+? matches any character (except newline)
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
4th Capturing group ('))
') matches the character ) literally
可以看到,第一个捕获组包含第二个捕获组。第二个捕获组是chalk
,第一个捕获组是[chalk](
。
- 你可以把你的javascript改成
console.log(md.replace(/('[.*?']'()(.+?)('))/g, '$2'))
重写你的正则表达式,删除捕获括号的圆括号,这样你只捕获它们里面的东西。
'[(.*?)']'((.+?)')
如果你是正则表达式的新手,我强烈推荐一个regex工具,比如regex101.com,看看你的组是什么,你的regex到底在做什么。
这是我为你保存的正则表达式https://regex101.com/r/tZ6yK9/1
使用此RegExp:
/'[([^']]+)'][^')]+')/g
如果你叫它
'and so use [chalk](#api).red(string[, options])'.replace(/'[([^']]+)'][^')]+')/g, '$1')
它返回这个
"所以用粉笔。红色(string[选项])"
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 如何使用jQuery自动打开页面上的所有链接
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- jquery动画可以通过编程链接吗
- 在单击href链接的同时下载文件
- 尽管链接成功并已成功下载,但未找到NPM模块
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 单击页面上的链接后高度发生变化
- 将纯文本URL转换为可单击链接
- python-selenium-点击上升链接
- 一点javascript元编程&可链接的设置器
- ng视图外的链接重定向到ng视图内的页面
- 如何调用“;链接_;在onclick事件上使用Javascript
- 链接所有<a>Meteor
- 如何在Angular中的表的所有单元格中添加链接
- 通过链接重定向不;我不在jstree中工作
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 单击超链接时,如何使用Google Maps API v3缩放地图
- JS Regex -替换markdown链接的内容