2012-07-20 29 views
0

我寫微件程序從彭博社網站 獲得金銀價格的價格這些都是劇本的部分:獲得銀或金率

function getPrice() { 

    var url = new URL(); 
    url.location = "http://www.bloomberg.com/markets/commodities/futures/"; 
    url.fetchAsync(PriceReturn); 
} 


function PriceReturn(url) { 

    var resultStr = url.result;  
    var subCheck = resultStr.substring(resultStr.indexOf("SILVER"));  
    foundStr = subCheck.match("[0-9]+\.[0-9]+"); 
    setPrice(parseFloat(foundStr).toFixed(2)); 
} 

我可以得到白銀價格(比方說現在27.30),但是當我想檢查黃金價格(.indexOf("GOLD"))(比方說現在1,585.60)

什麼是subcheck.match()腳本來獲得該價格?

+0

。什麼是'URL'? – Neal 2012-07-20 18:37:08

+0

[RegExp for Numbers in the thousandss]中可能有重複(http://stackoverflow.com/questions/8044278/regexp-for-numbers-in-the-thousands) – Dancrumb 2012-07-20 18:41:26

回答

2

您的問題是[0-9]+\.[0-9]+1,585.60之間的數字不符。正確的正則表達式是:

/\d{1,3}(?:,\d{3})*\.\d{2}/ 

這將格式化的數字與兩位小數匹配。

+0

\ d通常匹配'7'。請參閱:http://ideone.com/e82Qb – 2012-07-20 18:47:51

+0

@VamanKulkarni我認爲這只是在C#中發生,請檢查此[小提琴](http://jsfiddle.net/ww5GU/1/) – madfriend 2012-07-20 18:53:46

+0

謝謝,但我沒有工作它返回到null,任何幫助我將欣賞 – 2012-07-20 19:00:48

0

我覺得TE模式應該是這樣的:

/\d+(\,\d+)?\.\d{1,3}/ 
+0

與此pat瀝,我仍然只是得到「585.60」,而不是「1,585.60」 – 2012-07-20 19:42:30

+0

模式仍然無法識別「逗號」 – 2012-07-20 19:43:08

+0

你是什麼意思?你可以添加?:,所以該組將是非caputring,所以模式看起來像這樣:/\d+(?:\,\d+)?\.\d{1,3}/,然後foundStr應該是一個數組[「1,585.60」]。 – 2012-07-20 20:02:33