如何提取字符串的特定部分

时间:2012-11-16 19:34:34

标签: javascript

我在变量中有一堆代码作为字符串:

.... = 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

如何提取特定变量的值?

2 个答案:

答案 0 :(得分:1)

您可以使用regular expression

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

Demonstration

答案 1 :(得分:0)

如果MAX_727.location是单引号中字符串的唯一部分,则可以将字符串拆分为包含[ before text ,* text in quotes *,的数组文字后]:

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.