The HTML document in the source below has two textareas – the first one contains html documents location info (blue_suede_shoes.html|im_countin_on_you.html … etc.).
The “var dataDB” is a “two-dimensional” array with the following structure:
dataDB[i][0] – an “ID” number
dataDB[i][1] – Title (in this example it serves no particular function)
dataDB[i][2] – the location as mentioned in the beginning
I want to construct a function using the RegExp replace method that will search the first textarea contents for all these locations (dataDB[i][2]) and if found, they will be replaced with the corresponding ID number (dataDB[i][0]).
So, in the end, the second textarea must be populated with ID numbers instead of document locations…
In the example below, only the last location is replaced with its corresponding number however… Is this a wrong RegExp construction issue, or something else, I cannot figure out where is the problem… Hope someone can find it…
[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Lyrics URL convertion table</title>
</head>
<body>
<form id=”form1″ name=”form1″ method=”post” action=””>
<label>
<textarea name=”dataInput” id=”dataInput” cols=”45″ rows=”15″>
blue_suede_shoes.html|im_countin_on_you.html
i_got_a_woman.html|one_sided_love_affair.html
i_love_you_because.html|just_because.html
tutti_frutti.html|tryin_to_get_to_you.html
im_gonna_sit_right_down.html
ill_never_let_you_go_.html
blue_moon.html|money_honey.html
rip_it_up.html|love_me.html
when_my_blue_moon_turns_to_gold.html
long_tall_sally.html|first_in_line.html
paralyzed.html|so_glad_youre_mine.html
old_shep.html|ready_teddy.html
anyplace_is_paradise.html
hows_the_world_treating_you.html
how_do_you_think_i_feel.html
</textarea>
</label>
<label>
<textarea name=”dataOutput” id=”dataOutput” cols=”45″ rows=”15″></textarea>
</label>
<input name=”convertButton” type=”button” value=”Convert” onclick=”doConvertion();”/>
</form>
<script type=”text/javascript”>
var dataDB=[
[“10″,”Blue Suede Shoes”,”blue_suede_shoes.html”],
[“20″,”I’m Countin’ on You”,”im_countin_on_you.html”],
[“30″,”I Got a Woman”,”i_got_a_woman.html”],
[“40″,”One-sided Love Affair”,”one_sided_love_affair.html”],
[“50″,”I Love You Because”,”i_love_you_because.html”],
[“60″,”Just Because”,”just_because.html”],
[“70″,”Tutti Frutti”,”tutti_frutti.html”],
[“80″,”Tryin’ to Get to You”,”tryin_to_get_to_you.html”],
[“90″,”I’m Gonna Sit Right down and Cry (Over You)”,”im_gonna_sit_right_down.html”],
[“100″,”I’ll Never Let You Go (Little Darlin’)”,”ill_never_let_you_go_.html”],
[“110″,”Blue Moon”,”blue_moon.html”],
[“120″,”Money Honey”,”money_honey.html”],
[“130″,”Rip It Up”,”rip_it_up.html”],
[“140″,”Love Me”,”love_me.html”],
[“150″,”When My Blue Moon Turns to Gold Again”,”when_my_blue_moon_turns_to_gold.html”],
[“160″,”Long Tall Sally”,”long_tall_sally.html”],
[“170″,”First in Line”,”first_in_line.html”],
[“180″,”Paralyzed”,”paralyzed.html”],
[“190″,”So Glad You’re Mine”,”so_glad_youre_mine.html”],
[“200″,”Old Shep”,”old_shep.html”],
[“210″,”Ready Teddy”,”ready_teddy.html”],
[“220″,”Anyplace Is Paradise”,”anyplace_is_paradise.html”],
[“230″,”How’s the World Treating You?”,”hows_the_world_treating_you.html”],
[“240″,”How Do You Think I Feel”,”how_do_you_think_i_feel.html”]
];
function doConvertion(){
var rawData = document.getElementById(“dataInput”).value;
for (var i=0; i < dataDB.length; i++){
if (dataDB[i][2] != “”){
re = new RegExp(dataDB[i][2], “gim”);
bufferData = rawData.replace(re, dataDB[i][0]);
}
}
document.getElementById(“dataOutput”).value = bufferData;
}
</script>
</body>
</html>