0

I am trying to replace each set of wildcard symbols (**) with tags (<p></p>).

For example, if I have:

var stuff = array(
    "The color *blue*!!!!",
    "The color *red*!!!!",
    "The colors *red* and *blue*!!!!"
);

I want to output:

var stuff = array(
    "The color <p>blue</p>!!!!",
    "The color <p>red</p>!!!!",
    "The colors <p>red</p> and <p>blue</p>!!!!"
);

What would be the most efficient way to do this?

4

2 回答 2

3

为什么不只运行一个简单的循环:

for(var i=0; i < stuff.length; i++) {
   stuff[i] = stuff[i].replace(/[*]([^*]+)[*]/g, '<p>$1</p>');
}
于 2013-09-28T22:10:22.013 回答
1

尝试

var stuff = [
    "The color *blue*!!!!",
    "The color *red*!!!!",
    "The colors *red* and *blue*!!!!"
];


 var res  = stuff.map(function(o){
     return o.replace(/\*(.*?)\*/g,'<p>$1</p>');
 });

或者只是一个循环

 for(var i=0, len = stuff.length; i<len; i++){
      stuff[i] = stuff[i].replace(/\*(.*?)\*/g,'<p>$1</p>');
  }

小提琴

于 2013-09-28T22:11:41.110 回答