I need a function to extract key values from an string like this: <!-- Name:Peter Smith --><!-- Age:23 -->
I want to make an standard function to extract any value. The call is something like this: var name=ExtractValue(text,"Name");
This is my approach:
var text="<!-- Name:Peter Smith --><!-- Age:23 -->"; // Data to be scanned
var name=ExtractValue(text,"Name"); // Get the "Name" key value
var age=ExtractValue(text,"Age"); // Get the "Age" key value
document.getElementById("result").innerHTML = "Extracted name: ("+name+"), age: ("+age+")";
// Function for extranting key values
function ExtractValue(data,key){
// It's try to be like /Name:(.*)\s--/
var rx = "/"+key+"(.*)\s--/";
var values = rx.exec(data);
if(values.lenght>0){
return values[1];
} else{
return "";
}
}
How can I do this? Thanks for your time!