tags:

views:

51

answers:

4

input is a string (var str) something like this:

"<<hello there//abcd//1234>>"

I want to know how to best extract the information to have 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

+1  A: 

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

matpol
+1  A: 
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];
Pranay Rana
there's a mistake in the above I think:var myOldString ="<>" var myNewString = myOldString.replace("<<", ''); myNewString = myNewString.replace(">>", '');
matpol
there << not < in question
Pranay Rana
you could combine both the replaces and maybe even split - `"<<hello there//abcd//1234>>".replace(/^<<|>>$/g, '').split('//')`
Anurag
+1  A: 
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).

GenericTypeTea
+1 but please correct this... the last is now `1234>>`... should have been `1234` only..
Reigel
If you use `slice` instead of substring, you can use a negative value in the arguments: `value.slice(2, -2);`
Andy E
@Reigel - it works fine, what's the problem?
GenericTypeTea
@GenericTypeTea - here is a demo of what I mean... http://jsfiddle.net/5W7P3/ and also `substring` should be `substr`...
Reigel
@Riegel: no, it shouldn't. `substr` is not defined in the ECMAScript specification, and it doesn't work the same across all browsers. Change your fiddle to `substring` and it works: http://jsfiddle.net/5W7P3/1/
Andy E
No. substring is correct. http://www.w3schools.com/jsref/jsref_substring.asp. Here's my code, working fine: http://jsfiddle.net/CyeW3/
GenericTypeTea
ahhh okay... I got now the confusion part.. sorry @GenericTypeTea... thanks @Andy E's head...
Reigel
+4  A: 

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"]
Andy E
@Andy - regex wins again =) I should really learn to use it one day.
GenericTypeTea
@GenericTypeTea: Actually, I was writing an answer similar to yours, and then the alert bar popped up with your answer in it, I decided to quickly change my solution so it wasn't a duplicate answer lol ;-)
Andy E
Thanks i will try it later and come back here. Regex is very cool, but i still don't have the will to learn something what's not so friendly to understand. Be back
Johua