I have a javascript script where there are lines, a example is :
http://www.youtube.com/watch?v=FOeLP1WdjFc&feature=related
I want to extract the substring that is after "watch?=" and before "&feature", its pattern is:
1) it is after "watch?="
2) it is before "&feature",
3) it is a 11-character string
I know other scripts could be easy, but now I only need javascript, if it is really impossible, then php is acceptable, how to achieve this? thanks!
Try this (working example):
var s = "http://www.youtube.com/watch?v=FOeLP1WdjFc&feature=related";
var left = "watch?v=";
var right = "&feature";
var parsed = s.substring(s.indexOf(left) + left.length, s.indexOf(right));
console.log(parsed);
This is a very rough, quick answer so I'd recommend looking into regular expressions for a more scalable method.
You could use a regex to formulate your requirements:
var res = url.match(/watch?=(.{11})&feature/);
if (res != null)
return res[1];
Not that this would work (extract the content of the v
parameter), but it does what you described.
You can achieve what you need using JavaScript .indexOf()
and .substring()
methods.
var index = url.indexOf('?v=') + 3;
alert(url.substring(index, index + 11));
See this DEMO.
In PHP, you can use standard function parse_url()
to parse a URL and parse_str()
to parse query string to extract an individual GET parameter like v
.