[英]regex find string in brackets and replace
我有一个字符串: Hello there [@Some Person](#/path/to/person). How are you? Where is your coffee? [@Another Person](#/path/to/another) said you are not great at regex.
Hello there [@Some Person](#/path/to/person). How are you? Where is your coffee? [@Another Person](#/path/to/another) said you are not great at regex.
我想将[@Some Person](#/path/to/person)
类的所有模式替换为Some Person
。
因此,上述示例的最终输出应为: Hello there Some Person. How are you? Where is your coffee? Another Person said you are not great at regex.
Hello there Some Person. How are you? Where is your coffee? Another Person said you are not great at regex.
到目前为止,我有:
> var re = /\[@([\w\s]+)\](\(\S+\))/g
> str.match(re)
> ["[@Some Person](#/path/to/person)", "[@Another Person](#/path/to/another)"]
// matches but not quite what I want.
> str.replace(re, $1)
> "Hello there undefined. How are you? Where is your coffee? undefined said you are not great at regex."
// does not replace as expected.
这是我的正则表达式沙箱 。 我只想使用str.replace
获得我想要的东西。 不知道是否可能。 似乎应该如此。 请帮忙。
您忘记了$1
左右的双引号:
str.replace(re, "$1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.