/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] RegExp replace problem

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>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Aug 03.2008 — You keep on rewriting over bufferdata. You need something like this.<i>
</i>function doConvertion(){
var rawData = document.getElementById("dataInput").value;
[b]var bufferData = rawData;[/b]
for (var i=0; i &lt; dataDB.length; i++){
if (dataDB[i][2] != ""){
re = new RegExp(dataDB[i][2], "gim");
bufferData = [b]bufferData[/b].replace(re, dataDB[i][0]);
}
}
document.getElementById("dataOutput").value = bufferData;
}
or to simplify it down (removing a variable)<i>
</i>function doConvertion(){
var bufferData = document.getElementById("dataInput").value;
for (var i=0; i &lt; dataDB.length; i++){
if (dataDB[i][2] != ""){
re = new RegExp(dataDB[i][2], "gim");
bufferData = bufferData.replace(re, dataDB[i][0]);
}
}
document.getElementById("dataOutput").value = bufferData;
}


I'd probably use this myself, but it's optional.<i>
</i>function doConvertion(){
var bufferData = document.getElementById("dataInput").value.[b]toLowerCase()[/b];
for (var i=0; i &lt; dataDB.length; i++){
if (dataDB[i][2] != ""){
bufferData = bufferData.replace(dataDB[i][2].[b]toLowerCase()[/b], dataDB[i][0]);
}
}
document.getElementById("dataOutput").value = bufferData;
}
If you don't need it to be case sensitive, you can remove all the toLowerCase calls.
Copy linkTweet thisAlerts:
@Vassil_CatsarovauthorAug 03.2008 — Thank you Declan, I just discovered what was wrong by myself and was going to delete this thread... Here is my modification:
[CODE]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");
rawData = rawData.replace(re, dataDB[i][0]);
}

}

document.getElementById("dataOutput").value = rawData;
}[/CODE]


I was thinking do I have to escape the dots or any other reserved words when constructing the regular expression but to my surprise, it works the way it is left...
Copy linkTweet thisAlerts:
@Vassil_CatsarovauthorAug 03.2008 — Declan, why are you trying to avoid setting the g, i, and m flags in your last example? What are the advantages of using lover case conversion? Is this going to work globally or through all the lines of text in the textarea?
×

Success!

Help @Vassil_Catsarov spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...