2015-08-28 77 views
-2

.replace函数在这些RegExp上没有做任何事情。替换为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. 
+3

更新这个我没有看到任何'regex'这里 – Tushar

+0

你如何数组看起来像?你为什么不张贴? – Jai

+0

添加了输入和预期输出。 – Piyush

回答

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. 
+0

谢谢@Jai。我不知道我在哪里搞砸了,但没有发生。无论如何感谢您的帮助。 – Piyush

+0

不用客气@piyush。只是想知道你如何维护你的阵列? – Jai

+0

它不可能是更糟糕的...是在一个错误的数组上做它。感谢您指点。它的工作现在。万分感谢。 – Piyush