我试着这个
str = "@a@b";
str2 = str.replace(/@/g,'-[' + str + ']');
Run Code Online (Sandbox Code Playgroud)
输出应该是 - [a] - [b]
您可以搜索@而不是搜索@,然后替换该组.
/@([^@]+)/g
@ look for this character literately
( ) group, this is the part used for replacing -> $1 in replacement string
[^@]+ look for one or more not @ characters
g global flag, replace all found matches
Run Code Online (Sandbox Code Playgroud)
替换是由第一组完成的$1.
var str = "@a@b",
str2 = str.replace(/@([^@]+)/g,'-[$1]');
console.log(str2);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |