small jquery parser
input is a string (var str) something like this:
"<<hello there//abcd//1234>>"
I want to know how to best extract the information to h开发者_如何学Pythonave this:
var a = "hello there";
var b = "abcd";
var c = 1234;
case a: select everything after << until first // case b: select everything after first // until second // case c: select everything after second // until >>
Anybody knows a simple solution? THX
If you can be sure the inner strings will not contain /
, >
or <
, you can do this with a one-liner:
"<<hello there//abcd//1234>>".match(/[^<>/]+/g)
// -> ["hello there", "abcd", "1234"]
var value = "<<hello there//abcd//1234>>";
value = value.substring(2, value.length-2);
var array = value.split("//");
var a = array[0];
var b = array[1];
var c = array[2];
Or instead of substring, you can use value = value.slice(2, -2);
(thanks to
Andy E's head for the alternative).
split it into an array using the '//' as the delimiter. You then have an array of three elements. Then for the first element use substring to get rid of the first two characters and for the last element us substring again to get rid of the last two characters.
This uses javascript rather than jquery specifically
var myOldString ="<<hello there//abcd//1234>>"
var myNewString = myOldString.replace("<<", '');
myNewString = myOldString.replace(">>", '');
var arrayofstring = myNewString .split("///")
var a = arrayofstring[0];
var b = arrayofstring[1];
var c = arrayofstring[2];
精彩评论