Javascript 如何用HTML标记替换字符串中的文本?

Javascript 如何用HTML标记替换字符串中的文本?,javascript,text,replace,Javascript,Text,Replace,我有字符串: " Text01 [1] text02 [2] text03 [3] " 我需要将[1][2][3]替换为input type=“text” 我需要它来创建JavaScript填空测验 我做过这样的事情: $("span").click(function () { var input = $("<input>", { val: $(this).text(), type: "text" }); $(this).replaceWith(input)

我有字符串:

" Text01 [1] text02 [2] text03 [3] " 
我需要将
[1][2][3]
替换为
input type=“text”

我需要它来创建JavaScript填空测验

我做过这样的事情:

$("span").click(function () 
{
    var input = $("<input>", { val: $(this).text(), type: "text" }); 

    $(this).replaceWith(input);

    input.select();
});
$(“span”)。单击(函数()
{
变量输入=$(“”,{val:$(this).text(),键入:“text”});
$(此).replaceWith(输入);
input.select();
});

我必须在
[1][2][3]
之间添加一些标记,这是我在jQuery中不知道如何做的部分。

嗯。。。也许是这样的

var str = " Text01 [1] text02 [2] text03 [3] ";

str = str.replace(/\[[0-9]+\]/g, function(match) {
    return '<span id="'+match+'"><input type="text"></span>';
});

alert(str); //outputs Text01 <span id="[1]"><input type="text" /></span>...
var str=“Text01[1]text02[2]text03[3]”;
str=str.replace(/\[[0-9]+\]/g,函数(匹配){
返回“”;
});
警报(str)//输出文本01。。。

那么,你试过什么?这是他的工作,但不是我真正想要的。。。我想用唯一的ID在[1]之间换行。。。因此,我可以检索我替换的值…尝试使用更新的代码,它将[1]、[2]或[3]设置为span的id。