将字符替换为标记(例如 *hi* 到 <p>hi</p>)
Replace character with tags (ex. *hi* to <p>hi</p>)
我正在尝试用标签(<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>');
}
尝试
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>');
}
小提琴
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 我如何找到一个句子中的所有空格并替换忽略它们
- 如何用更合适的内容替换document.write
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 在DOM中查找一个模式并替换它's的内容使用jquery
- 用空格替换下划线PHP
- str.split(someString).join(someOtherString)是否等效于替换
- 使用javascript的图像替换循环
- window.location替换并传递URL历史记录条目中的变量
- 替换标记中的属性
- 指令的模板必须只有一个根元素:With restrict E&替换true
- AngularJs对ng消息的自定义替换
- 用超链接替换URLS
- 用cdata标记替换脚本标记
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 在javascript中,I'我很难弄清楚如何让regex只替换捕获而不替换匹配
- 替换url中变量的值
- 我希望在不替换现有变量的情况下恢复localStorage中的变量
- Wordpress:替换源(域名)链接
- 将字符替换为标记(例如 *hi* 到
hi
)