Javascript/jQuery替换重复使用span标记

Javascript/jQuery替换重复使用span标记,javascript,jquery,regex,Javascript,Jquery,Regex,我不擅长正则表达式,在这个问题上我需要帮助 1) 我想模仿bbcode,替换html文本中的span标记 HTML格式 2) 如果可能的话,还可以在开始时从最后一种格式恢复为源HTML格式 谢谢。那么: thtmk.replace(/\<span style\=\"color\: \#000\;\"\>/g,'[') thtmk.replace(/\<\/span\>/g,']') thtmk.replace(/\/g,[')) thtmk.replace(/\/g,'

我不擅长正则表达式,在这个问题上我需要帮助

1) 我想模仿bbcode,替换html文本中的span标记

HTML格式 2) 如果可能的话,还可以在开始时从最后一种格式恢复为源HTML格式

谢谢。

那么:

thtmk.replace(/\<span style\=\"color\: \#000\;\"\>/g,'[')
thtmk.replace(/\<\/span\>/g,']')
thtmk.replace(/\/g,['))
thtmk.replace(/\/g,']'))
它对你有用吗?

怎么样:

thtmk.replace(/\<span style\=\"color\: \#000\;\"\>/g,'[')
thtmk.replace(/\<\/span\>/g,']')
thtmk.replace(/\/g,['))
thtmk.replace(/\/g,']'))

它对您有用吗?

使用正则表达式

HTML页面

<div id="t">this the text [content color this text] with mutiple span like this [second span] and more;</div>
<br/>
<input type="button" id="button1" value="submit" />
<div id="t1"></div>
<br/>
此文本[内容颜色此文本]具有多个跨度,如此[第二个跨度]及更多;


jQuery

$(document).ready(function () {
    $('#button1').click(function () {
        var thtml = $('#t').text();

        var thtml1 = thtml.replace(/\[/g, '<span style="color: blue;">');
        var thtml2 = thtml1.replace(/\]/g, '</span>');
        $('#t1').html(thtml2);

    });

});
$(文档).ready(函数(){
$('#按钮1')。单击(函数(){
var thtml=$('#t').text();
var thtml1=thtml.replace(/\[/g',);
var thtml2=thtml1.replace(/\]/g');
$('#t1').html(thtml2);
});
});
它替换所有出现的“[”和“]”


使用正则表达式的工作演示

HTML页面

<div id="t">this the text [content color this text] with mutiple span like this [second span] and more;</div>
<br/>
<input type="button" id="button1" value="submit" />
<div id="t1"></div>
<br/>
此文本[内容颜色此文本]具有多个跨度,如此[第二个跨度]及更多;


jQuery

$(document).ready(function () {
    $('#button1').click(function () {
        var thtml = $('#t').text();

        var thtml1 = thtml.replace(/\[/g, '<span style="color: blue;">');
        var thtml2 = thtml1.replace(/\]/g, '</span>');
        $('#t1').html(thtml2);

    });

});
$(文档).ready(函数(){
$('#按钮1')。单击(函数(){
var thtml=$('#t').text();
var thtml1=thtml.replace(/\[/g',);
var thtml2=thtml1.replace(/\]/g');
$('#t1').html(thtml2);
});
});
它替换所有出现的“[”和“]”


工作演示

你能发布你试用过的代码吗?你能发布你试用过的代码吗?在斜杠之间,并在末尾添加一个g是一种方法。g表示字符串中的全局或所有实例。谢谢,它正在工作。。我可以通过以下命令恢复到html:
thtml.replace(/\[/g');
thtml.replace(/\]/g')在斜杠之间,并在末尾添加一个g就是这样做的方法。g表示字符串中的全局或所有实例。谢谢,它正在工作。。我可以通过以下命令恢复到html:
thtml.replace(/\[/g');
thtml.replace(/\]/g')
$(document).ready(function () {
    $('#button1').click(function () {
        var thtml = $('#t').text();

        var thtml1 = thtml.replace(/\[/g, '<span style="color: blue;">');
        var thtml2 = thtml1.replace(/\]/g, '</span>');
        $('#t1').html(thtml2);

    });

});