用RegExp替换不起作用

时间:2015-08-28 10:03:33

标签: jquery regex performance

.replace函数在这些RegExp上没有做任何事情。

(((array[0].replace("%%_%", "<ol><li>")).replace("%_%%", "</li></ol>")).replace("%_%", "<li></li>"))

非常感谢任何帮助。 (使用jquery 1.6.2)

输入:

Ought these are balls place mrs their times add she. Taken no great widow spoke of it small. Genius use except son esteem merely her limits. %%\_%Sons park by do make on. It do oh cottage offered cottage in written%\_%Especially of dissimilar up attachment themselves by interested boisterous%\_%Linen mrs seems men table.Jennings dashwood to quitting marriage bachelor in%_%On as conviction in of appearance apartments boisterous.%\_%%

输出:

Ought these are balls place mrs their times add she. Taken no great widow spoke of it small. Genius use except son esteem merely her limits.
 1. Sons park by do make on. It do oh cottage offered cottage in written
 2. Especially of dissimilar up attachment themselves by interested
    boisterous
 3. Linen mrs seems men table. Jennings dashwood to quitting
    marriage bachelor in On as conviction in of appearance apartments
    boisterous.

1 个答案:

答案 0 :(得分:2)

使用正则表达式而不是传递字符串。并确保使用g全球标志。

var arr = ["Ought these are balls place mrs their times add she. Taken no great widow spoke of it small. Genius use except son esteem merely her limits. %%_%Sons park by do make on. It do oh cottage offered cottage in written%_%Especially of dissimilar up attachment themselves by interested boisterous%_%Linen mrs seems men table. Jennings dashwood to quitting marriage bachelor in%_%On as conviction in of appearance apartments boisterous.%_%%"];

var newArr = (((arr[0].replace(/%%_%/g, "<ol><li>")).replace(/%_%%/g, "</li></ol>")).replace(/%_%/g, "</li><li>"));

document.body.innerHTML = newArr;

注意:

您必须在上一个.replace

中更新此内容
.replace(/%_%/g, "</li><li>")
//----------------^^^^^^^^^----put the closing first then the opening.