Javascript 正则表达式,提取键之间的文本
给定:Javascript 正则表达式,提取键之间的文本,javascript,regex,Javascript,Regex,给定: This is some text which could have line breaks and tabs before and after {code} and I want them {code} to be replaced {code}in pairs{code} without any issues. This is some text which could have line breaks and tabs before and after <code>
This is some text which could have
line breaks and tabs before and after {code}
and I want them {code} to be replaced {code}in pairs{code} without any issues.
This is some text which could have
line breaks and tabs before and after <code>
and I want them </code> to be replaced <code>in pairs</code> without any issues.
var sample1 = 'test test test {code}foo bar{code} {code}good to know{code}';
var regEx1 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r1 = sample1.replace(regEx1, '<code>$2</code>');
var sample2 = 'test test test {code}\tfoo bar{code} {code}\r\ngood to know{code}';
var regEx2 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r2 = sample2.replace(regEx2, '<code>$2</code>');
test test test {code} foo bar{code} {code}
good to know{code}
我想要:
This is some text which could have
line breaks and tabs before and after {code}
and I want them {code} to be replaced {code}in pairs{code} without any issues.
This is some text which could have
line breaks and tabs before and after <code>
and I want them </code> to be replaced <code>in pairs</code> without any issues.
var sample1 = 'test test test {code}foo bar{code} {code}good to know{code}';
var regEx1 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r1 = sample1.replace(regEx1, '<code>$2</code>');
var sample2 = 'test test test {code}\tfoo bar{code} {code}\r\ngood to know{code}';
var regEx2 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r2 = sample2.replace(regEx2, '<code>$2</code>');
test test test {code} foo bar{code} {code}
good to know{code}
JsFiddle:
简单工作文本示例:
This is some text which could have
line breaks and tabs before and after {code}
and I want them {code} to be replaced {code}in pairs{code} without any issues.
This is some text which could have
line breaks and tabs before and after <code>
and I want them </code> to be replaced <code>in pairs</code> without any issues.
var sample1 = 'test test test {code}foo bar{code} {code}good to know{code}';
var regEx1 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r1 = sample1.replace(regEx1, '<code>$2</code>');
var sample2 = 'test test test {code}\tfoo bar{code} {code}\r\ngood to know{code}';
var regEx2 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r2 = sample2.replace(regEx2, '<code>$2</code>');
test test test {code} foo bar{code} {code}
good to know{code}
给出:
test test test <code>foo bar</code> <code>good to know</code>
非工作样本:
This is some text which could have
line breaks and tabs before and after {code}
and I want them {code} to be replaced {code}in pairs{code} without any issues.
This is some text which could have
line breaks and tabs before and after <code>
and I want them </code> to be replaced <code>in pairs</code> without any issues.
var sample1 = 'test test test {code}foo bar{code} {code}good to know{code}';
var regEx1 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r1 = sample1.replace(regEx1, '<code>$2</code>');
var sample2 = 'test test test {code}\tfoo bar{code} {code}\r\ngood to know{code}';
var regEx2 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r2 = sample2.replace(regEx2, '<code>$2</code>');
test test test {code} foo bar{code} {code}
good to know{code}
给出:
This is some text which could have
line breaks and tabs before and after {code}
and I want them {code} to be replaced {code}in pairs{code} without any issues.
This is some text which could have
line breaks and tabs before and after <code>
and I want them </code> to be replaced <code>in pairs</code> without any issues.
var sample1 = 'test test test {code}foo bar{code} {code}good to know{code}';
var regEx1 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r1 = sample1.replace(regEx1, '<code>$2</code>');
var sample2 = 'test test test {code}\tfoo bar{code} {code}\r\ngood to know{code}';
var regEx2 = new RegExp('(\{code\})(.*?)(\{code\})', 'gi');
var r2 = sample2.replace(regEx2, '<code>$2</code>');
test test test {code} foo bar{code} {code}
good to know{code}
看起来您只需要正确地转义第一个
{
,并使用正则表达式文本来修复在字符串中重复转义反斜杠的需要:
/(\{code\})([\s\S]*?)(\{code\})/gi
请注意,您甚至不需要在{code}
s周围使用捕获括号:
/\{code\}([\s\S]*?)\{code\}/gi
@yckart,OP中没有任何东西表明它需要。是的,你完全正确!这只是一个旁注;)这是我的修正:
/\{\s*code\s*}([\s\s]*?)\{\s*code\s*}/gi