I am looking for a solution to extract key values from a string that looks like this: <!-- Name:Peter Smith --><!-- Age:23 -->
My goal is to create a standard function that can extract any value needed. The function call would be something like: var name=ExtractValue(text,"Name");
Here is my proposed 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 extracting key values
function ExtractValue(data,key){
// It should work similarly to /Name:(.*)\s--/
var rx = "/"+key+"(.*)\s--/";
var values = rx.exec(data);
if(values.length>0){
return values[1];
} else{
return "";
}
}
Any suggestions on how I can achieve this? Thanks for your help!