Saymon
Saymon

Reputation: 520

How to extract a specific part of a string

I have this bunch of code in a variable as string:

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

(I added dots at the begining and end to make it easier to read)

This code (which is being manipulated as string) contains a variable value, MAX_727.location.

How can I extract the value of that specific variable ?

Upvotes: 1

Views: 181

Answers (2)

Curlystraw
Curlystraw

Reputation: 229

If MAX_727.location is the only part of the string that is in single quote marks, you can split the string into an array containing [before text,*text in quotes*,after text]:

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.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382122

You may use a regular expression :

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

Demonstration

Upvotes: 1

Related Questions