Javascript 用标记替换字符(例如*hi*到<;p>;hi<;/p>;)
我试图用标记(Javascript 用标记替换字符(例如*hi*到<;p>;hi<;/p>;),javascript,regex,Javascript,Regex,我试图用标记()替换每组通配符(***) 例如,如果我有: 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>!!!
)替换每组通配符(***
)
例如,如果我有:
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>!!!!"
);
我想输出:
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>!!!!"
);
var stuff=array(
“颜色蓝色!!”,
“颜色红色!!”,
“颜色红色和蓝色!!”
);
最有效的方法是什么?为什么不运行一个简单的循环:
for(var i=0; i < stuff.length; i++) {
stuff[i] = stuff[i].replace(/[*]([^*]+)[*]/g, '<p>$1</p>');
}
for(var i=0;i');
}
为什么不运行一个简单的循环:
for(var i=0; i < stuff.length; i++) {
stuff[i] = stuff[i].replace(/[*]([^*]+)[*]/g, '<p>$1</p>');
}
for(var i=0;i');
}
试试看
var stuff=[
“颜色*蓝色*!!”,
“颜色*红色*!!”,
“颜色*红色*和*蓝色*!!”
];
var res=stuff.map(函数(o){
返回o.replace(/\*(.*?\*/g,$1');
});
或者只是一个循环
for(var i=0, len = stuff.length; i<len; i++){
stuff[i] = stuff[i].replace(/\*(.*?)\*/g,'<p>$1</p>');
}
for(var i=0,len=stuff.length;iTry
var stuff=[
“颜色*蓝色*!!”,
“颜色*红色*!!”,
“颜色*红色*和*蓝色*!!”
];
var res=stuff.map(函数(o){
返回o.replace(/\*(.*?\*/g,$1');
});
或者只是一个循环
for(var i=0, len = stuff.length; i<len; i++){
stuff[i] = stuff[i].replace(/\*(.*?)\*/g,'<p>$1</p>');
}
for(var i=0,len=stuff.length;i你能发布你的代码吗?你尝试了什么?还有,什么是array
?我们能假设array===array.of
?你能发布你的代码吗?你尝试了什么?还有,array
?我们能假设array==array.of
??