我试图用标签( **
)替换每组通配符( <p></p>
)。
例如,如果我有:
var stuff = array(
"The color *blue*!!!!",
"The color *red*!!!!",
"The colors *red* and *blue*!!!!"
);
我要输出:
var stuff = array(
"The color <p>blue</p>!!!!",
"The color <p>red</p>!!!!",
"The colors <p>red</p> and <p>blue</p>!!!!"
);
最有效的方法是什么?
最佳答案
为什么不只运行一个简单的循环:
for(var i=0; i < stuff.length; i++) {
stuff[i] = stuff[i].replace(/[*]([^*]+)[*]/g, '<p>$1</p>');
}
关于javascript - 用标签替换字符(例如 *hi* 到 <p>hi</p>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072149/