2012-11-16 52 views
1

我有一個變量作爲字符串這一串代碼:如何提取字符串中的特定部分

.... = 40; var posY = 40; MAX_727.moveTo(posX, posY); } MAX_727.location='http://one.cam4ads.com/www/delivery/ac.php?bannerid=727&zoneid=19&target=_blank&withtext=0&source=&timeout=0&ct0='; MAX_727.blur(); window.focus... 

(我加在開始時和結束點,以使其更易於閱讀)

此代碼(正在作爲字符串操作)包含變量值MAX_727.location

如何提取特定變量的值?

回答

1

你可以使用一個regular expression

var value = /MAX_727\.location=\'([^\']*)/.exec(s)[1]; 

Demonstration

+0

感謝破壞,這個作品完美! – Saymon

+0

不客氣。請注意,如果您不確定該字符串中是否包含您要查找的內容,則應該檢查'exec'函數返回的數組長度。 –

0

如果MAX_727.location的是,在單引號字符串的只是一部分,你可以將字符串分割成包含數組[ 文本之前,*引號的文本*,後文]:

var codeString = "your String goes here"; 
var codeArray = codeString.split("'"); //Split the String at every ' mark, and add it to an array 
var location = codeArray[1]; //Get the second value in the array, or the part of the String that was in quotes.