2009-01-02 149 views
58

我用一個簡單的正則表達式用換行來代替休息標籤:的JavaScript相當於PHP的preg_replace函數

br_regex = /<br>/; 
input_content = input_content.replace(br_regex, "\n"); 

這將只替換休息標籤的第一個實例,但我需要全部更換。 preg_match_all()會在PHP中實現,但我想知道JavaScript的等價物。

+3

當你不需要匹配一個正則表達式,你應該使用str_replace函數(),而不是preg_match_all(),例如: $ str = str_replace('
',「\ n」,$ str); – scronide 2009-01-02 19:30:22

+0

你應該真的改變標題說preg_replace而不是preg_match_all,這是混亂,並沒有幫助時搜索 – iBobo 2010-07-01 17:15:09

回答

105

使用全局標誌,g

foo.replace(/<br>/g,"\n") 
17

JS成語非正則表達式全局替換:

input_content.split('<br>').join('\n')