Jquery 正则表达式未按预期工作
写了一些代码来帮助 这是小提琴: 为什么每个正则表达式只运行一次,您将如何处理嵌套的条目Jquery 正则表达式未按预期工作,jquery,regex,Jquery,Regex,写了一些代码来帮助 这是小提琴: 为什么每个正则表达式只运行一次,您将如何处理嵌套的条目 <div id="texts"> **this** **would** make it **bold** __this would__ make it __underlined__ __**how about bold and underlined**__ and **__the other way around__** </div> **这个****会**加上**粗体*
<div id="texts">
**this** **would** make it **bold** __this would__ make it __underlined__
__**how about bold and underlined**__
and
**__the other way around__**
</div>
**这个****会**加上**粗体**\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu__
__**加粗加下划线怎么样**__
和
**__反过来__**
var res={
粗体:/\*\*(.*?\*\**/g,
下划线:/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
$。每个(res,函数(type,re){
var s=$(“#文本”).html();
var m=执行董事;
发现的var=m[0];
$.each(m,函数(索引,值){
如果(索引==0)返回true;
var html=s.replace(找到,“+value+”,“g”);
$(“#文本”).html(html);
});
});
那代码太复杂了。试试这个:
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
$.each(res, function(type, re) {
var txt = $( "#texts" ).html();
$( "#texts" ).html( txt.replace( re, '<span class="'+type+'" >$1</span>' ) );
});
var res={
粗体:/\*\*(.*?\*\**/g,
下划线:/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
$。每个(res,函数(type,re){
var txt=$(“#文本”).html();
$(“#text”).html(txt.replace(re,$1');
});
那代码太复杂了。试试这个:
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
$.each(res, function(type, re) {
var txt = $( "#texts" ).html();
$( "#texts" ).html( txt.replace( re, '<span class="'+type+'" >$1</span>' ) );
});
var res={
粗体:/\*\*(.*?\*\**/g,
下划线:/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
$。每个(res,函数(type,re){
var txt=$(“#文本”).html();
$(“#text”).html(txt.replace(re,$1');
});
这是因为您要反复替换找到的第一个值。如果检查结果,您会发现您已经用多个标记包装了第一个匹配项
您正在执行一个双循环,首先定位匹配项,然后替换每个匹配项。你可以马上更换它们。此外,每次迭代时,您都会取出html并将其放回原处,此时您只需执行一次:
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
var s = $("#texts").html();
$.each(res, function(type, re) {
s = s.replace(re,'<span class="'+type+'" >$1</span>');
});
$("#texts").html(s);
var res={
粗体:/\*\*(.*?\*\**/g,
下划线:/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
var s=$(“#文本”).html();
$。每个(res,函数(type,re){
s=s.替换(重新命名,$1');
});
$(“#文本”).html;
这是因为您要反复替换找到的第一个值。如果检查结果,您会发现您已经用多个标记包装了第一个匹配项
您正在执行一个双循环,首先定位匹配项,然后替换每个匹配项。你可以马上更换它们。此外,每次迭代时,您都会取出html并将其放回原处,此时您只需执行一次:
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
var s = $("#texts").html();
$.each(res, function(type, re) {
s = s.replace(re,'<span class="'+type+'" >$1</span>');
});
$("#texts").html(s);
var res={
粗体:/\*\*(.*?\*\**/g,
下划线:/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
var s=$(“#文本”).html();
$。每个(res,函数(type,re){
s=s.替换(重新命名,$1');
});
$(“#文本”).html;
像这样的嵌套问题是不规则上下文无关语法的教科书示例。正则表达式无法可靠地解析它们,您需要一个FSM。不,不是。像这样的嵌套问题是不规则上下文无关语法的教科书示例。正则表达式无法可靠地解析它们,您需要一个FSM。不,不是。那每次都会替换这些东西。Guffa的不知道你的意思,但我同意Guffa的解决方案更优雅。那每次也会替换这些东西。Guffa的不知道你的意思,但我同意Guffa的解决方案更优雅。@mplungjan:我只是使用了错误的变量来放回html。我知道。固定的。现在原来的问题变得更难了——我的新提琴又失败了:(@mplungjan:您正试图使用$(“#text”)
访问带有class=“text”的元素
。将其更改为id或更改选择器。@mplungjan:我只是使用了错误的变量将html放回。我知道。修复了。现在原来的问题变得更难了-我的新提琴再次失败:(@mplungjan:您正试图使用$('#text')
访问带有class=“text”的元素
。将其更改为id或更改选择器。