2013-10-18 24 views
2

糾正我,如果我錯了,但我相信正則表達式({|})傳統上會匹配一個{或a}。但是,當我有一個這樣的字符串:「{你好,我是一個字符串}」,我調用這個函數:非傳統或在Javascript中匹配正則表達式?

var album = $(song).data('album').replace(/({|})/, '', 'g'); 

只有{被替換,離開尾}。是什麼賦予了?

回答

2

我相信如果第一個參數是一個正則表達式對象,則會忽略非標準flags參數。按照MDN

To perform a global search and replace, either include the g switch in the 
regular expression or if the first parameter is a string, include g in the 
flags parameter. 

對於你的榜樣,下面的工作:

> "{hello}".replace(/({|})/g, '') 
> "hello"