Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找字符并用HTML换行_Javascript_Jquery_Html_Regex - Fatal编程技术网

Javascript 查找字符并用HTML换行

Javascript 查找字符并用HTML换行,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我试图在文档中找到/(斜杠),并用span将其包装起来 我试过了 var slashes = "//"; /slashes+/ 因此,输出应为: Hello There! I Am <span class="slashes">//</span> An Alien 您需要避开正则表达式中的斜杠。试一试 var mystring = "adjfadfafdas//dsagdsg//dsafda" mystring.replace(/\/\//g,'<span cla

我试图在文档中找到
/
(斜杠),并用
span
将其包装起来

我试过了

var slashes = "//";
/slashes+/
因此,输出应为:

Hello There! I Am <span class="slashes">//</span> An Alien

您需要避开正则表达式中的斜杠。试一试

var mystring = "adjfadfafdas//dsagdsg//dsafda"
mystring.replace(/\/\//g,'<span class="slashes">\/\/</span>');
var mystring=“adjfadfadas//dsagdsg//dsafda”
replace(//\//\//g,“\//\”);
应该输出

"adjfadfafdas<span class="slashes">//</span>dsagdsg<span class="slashes">//</span>dsafda"
“adjfadfdas//dsagdsg//dsafda”
如果要替换h2和p标记中的斜杠,可以这样循环:

$('h2, p').each(function(i, elem) { 
    $(elem).text(
        $(elem).text().replace(/\/\//g,'<span class="slashes">\/\/</span>'));
});
$('h2,p')。每个(函数(i,elem){
$(elem).text(
$(elem.text().replace(//\//\//g,'\//\//');
});

不过,这会使您在p和h2标记中可能拥有的任何附加html标记消失。

如果您只想将此类替换应用于单个
/
,请使用

mystring = mystring.replace(/(\/{2})/g, "<span class=\"slashes\">$1</span>");

测试代码。

这是另一种方法

//Find html inside element with id content
var html = $('#content').html();
//Replace // with <span style='color:red'>//</span>
html = html.replace(/\/{2}/g,"<span style='color:red'>$&</span>");
//Return updated html back to DOM
$('#content').html(html);​
//在id为content的元素中查找html
var html=$('#content').html();
//替换//为//
html=html.replace(/\/{2}/g,“$&”);
//将更新的html返回到DOM
$('#content').html(html);​

这是我想你找对地方了。唯一需要修复的是正则表达式:

.replace(/\/\//g, '<span class="slashes">$1</span>'));
.replace(//\//\//g,$1');

关注文本节点(类型3)很重要,而不是全局替换可能会破坏页面的HTML正文。

oh wow,g'luck,这需要在索引值内进行字符搜索和替换,诸如此类。。。。祝你好运!regexp应该是/\/\//g,但是它需要的远不止一个简单的.replace。如何在jsfiddle.net上共享您当前的尝试?如果使用外部字符串来构建regex,则需要双重转义文本,然后构建一个显式regexp对象。如果有帮助,斜杠旁边不会有任何字符。那么我该如何对整个文档执行此操作呢?主要在h2和p元素$('h2,p')中。每个元素(函数(i,elem){$(elem).text($(elem).text().replace(//\//\//\//g,'\//\'););选择其中的每一个并循环它们是最安全的方法。使用此方法,HTML呈现为文本并显示在页面上。
mystring = mystring.replace(/((?:\/{2})+)/g, "<span class=\"slashes\">$1</span>");
//Find html inside element with id content
var html = $('#content').html();
//Replace // with <span style='color:red'>//</span>
html = html.replace(/\/{2}/g,"<span style='color:red'>$&</span>");
//Return updated html back to DOM
$('#content').html(html);​
.replace(/\/\//g, '<span class="slashes">$1</span>'));