Click to See Complete Forum and Search --> : Clever way to get this data out of string ### |
whatsInaName
07-16-2008, 07:55 PM
Basically I have a string were I want to get the first four instances of: # |
Were # = a digit, can be from 0 to 99999
Examples:
<some html> 23 | <some more html> <yet more> 4 | <fgdfg> 3412 | <fgdfgsdf>
Seems one way I can do it is look for a digit(s) followed by a space then a "|" then add that to a var, then replace that with junk, then repeat... two biggest problems with that is the fact its messy and it will break if there are dupes. (edit: well replace only does the first occurrence it finds, so I guess that would work...)
Declan1991
07-16-2008, 08:12 PM
Something like:
alert("<some html> 23 | <some more html> <yet more> 4 | <fgdfg> 3412 | <fgdfgsdf>".match(/\d{1,5}\s\|/g).join("\n"));
You'll have to do the first four thing manually, and if you only want the number, you'll have to remove the last two characters, but it will work.
whatsInaName
07-16-2008, 08:14 PM
yeah its working right now, but its nasty
var CostWood = junkCost.match(/\d+\s\|/);
junkCost = junkCost.replace(CostWood, "");
CostWood = String(CostWood).match(/\d+/);
var CostClay = junkCost.match(/\d+\s\|/);
junkCost = junkCost.replace(CostClay, "");
CostClay = String(CostClay).match(/\d+/);
var CostIron = junkCost.match(/\d+\s\|/);
junkCost = junkCost.replace(CostIron, "");
CostIron = String(CostIron).match(/\d+/);
var CostCrop = junkCost.match(/\d+\s\|/);
junkCost = junkCost.replace(CostCrop, "");
CostCrop = String(CostCrop).match(/\d+/);
dragle
07-17-2008, 09:09 AM
You could add a slice to pull out the values you want:
var theArray = "<A> 23 | <1> <2> 4 | <3> 3412 | <4>".match(/\d{1,5}\s\|/g);
alert((theArray) ? theArray.slice(0,4).join('') : 'No match');
Good luck!
Declan1991
07-17-2008, 11:49 AM
var junkCost = "<some html> 23 | <some more html> <yet more> 4 | <fgdfg> 3412 | <fgdfgsdf>";
var Costs = junkCost.match(/\d+\s\|/g);
var CostWood = Costs[0].match(/\d+/);
var CostClay = Costs[1].match(/\d+/);
var CostIron = Costs[2].match(/\d+/);
//var CostCrop = Costs[3].match(/\d+/);
alert(CostWood+"\n"+CostClay+"\n"+CostIron);Still not ideal, but better.