2012-11-16 120 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.