2015-08-28 79 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