Javascript - 替换括号之间的字符串,但括号应该保留
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13247864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - replace string between brackets, but the brackets should stay
提问by Peter O.
I'd like to replace character inside a string, e.g.
我想替换字符串中的字符,例如
Drafts [2]
草稿 [ 2]
To:
到:
Drafts [3]
草稿 [ 3]
This regex returns only Drafts 3:
此正则表达式仅返回 Drafts 3:
str.replace(/\[(.+?)\]/g, 3)
Thanks for help in advance
提前感谢您的帮助
回答by anishsane
Do you need something more than below?
您需要比以下更多的东西吗?
var num=2 // parse this from drafts [2]
num++;
var newstr=str.replace(/\[(.+?)\]/g, "["+num+"]")
Or the brackets can change to <> {} per input?
或者括号可以更改为 <> {} 每个输入?
You can also give a function instead of the replace-string.
您还可以提供一个函数而不是替换字符串。
var str = "Drafts [2]";
function replacer(match, p1, p2, p3, offset, string) {
return p1 + (1+parseInt(p2)) + p3;
}
var newstr=str.replace(/([\[(])(.+?)([\])])/g, replacer);
alert(newstr); // alerts "Drafts [3]"
回答by Asad Saeeduddin
Use zero width assertionsinstead of actually matching the brackets.
使用零宽度断言而不是实际匹配括号。
EDIT: Javascript does not have lookbehind. :c
编辑:Javascript 没有后视。:C
As a general solution, you could capture the surrounding content and put it back in the replacement string using backreferences.
作为通用解决方案,您可以捕获周围的内容并使用反向引用将其放回替换字符串中。
str.replace(/(\[).+?(\])/g, "")
Alternatively, you could include hardcoded brackets in your replacement.
或者,您可以在替换中包含硬编码的括号。
回答by smokiespartakus
You could just add the brackets to the replacement text like this:
您可以像这样将括号添加到替换文本中:
str.replace(/\[(.+?)\]/g, "["+3+"]")
Edit: If you need to do anything with the number in the brackets, you can use a function instead of the replacement text:
编辑:如果您需要对括号中的数字执行任何操作,则可以使用函数代替替换文本:
str.replace(/\[(.+?)\]/g, function(string, first){
// string is the full result of the regex "[2]"
//first is the number 2 from "draft [2]"
return "["+(first++)+"]";
})
回答by Denys Rusov
If you need such functionality more then once you may create function with method borrowingtrick.
如果您需要更多这样的功能,那么您可以使用方法借用技巧创建函数。
const args = {
source: "Lorem [ipsum] dolor [sit] amet",
prefix: `<i style="background-color:yellow">`,
suffix: '</i>',
global: true,
deleteBrackets: true
}
const bracketsToTags = (args) => {
const { source, prefix, suffix, global = true, deleteBrackets = false } = args;
const reg = /\[(.+?)\]/;
source.replace = ''.replace;
return source.replace(new RegExp(reg, global ? 'g': ''),
(withBrackets, withoutBrackets) => (
`${prefix}${deleteBrackets ? withoutBrackets: withBrackets}${suffix}`
))
}
So, running function
所以,运行函数
bracketsToTags(args)
you'll get
你会得到
"Lorem <i style="background-color:yellow">ipsum</i> dolor <i style="background-color:yellow">sit</i> amet"
PS: use 'React HTML Parser' for React - link
PS:对 React 使用“React HTML Parser” -链接