PDA

View Full Version : String.replace() Not Replacing


Kenji Miyamoto
07-20-2005, 08:48 PM
I currently am having difficulties relating to the following script:function storyDiv(inp) {
mid = inp.replace(/<story class\"(h|n|s)\" title=\"(.*?)\">(.*?)<\/story>/, "<div class=\"story\$1\" xml:lang=\"en-US\" title=\"\$2\"\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

out = mid.replace(/<story title=\"(.*?)\" class\"(h|n|s)\">(.*?)<\/story>/, "<div class=\"story\$2\" xml:lang=\"en-US\" title=\"\$1\"\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

return out;
}I give it the following input, and it returns the exact same: <story class="h" title="Cows Have Fun">
<p>I daresay!</p>
</story>I intend it to change the code to this: <div class="storyh" title="Cows Have Fun">
<h3 class=\"title\">Cows Have Fun</h3>
<div class="storyData">
<p>I daresay!</p>
</div>
</div>Where is the error in my code?

medigerati
07-20-2005, 09:00 PM
i don't know if this fixes it but your missing some equal signs... here they are added:

(with smiley faces to show where they were missing)

function storyDiv(inp) {
mid = inp.replace(/<story class= :eek: \"(h|n|s)\" title=\"(.*?)\">(.*?)<\/story>/, "<div class=\"story\$1\" xml:lang=\"en-US\" title=\"\$2\"\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

out = mid.replace(/<story title=\"(.*?)\" class= :eek: \"(h|n|s)\">(.*?)<\/story>/, "<div class=\"story\$2\" xml:lang=\"en-US\" title=\"\$1\"\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

return out;
}

Kenji Miyamoto
07-20-2005, 09:37 PM
I can't believe I forgot those equal signs. Though, the results of the script are the same.

Willy Duitt
07-21-2005, 01:19 AM
http://forums.devshed.com/t273743/s712befce71929a4b28ae1e99b5b3b269.html
http://forums.devshed.com/t273453/s712befce71929a4b28ae1e99b5b3b269.html

Kenji Miyamoto
07-21-2005, 04:52 AM
What are you saying with these URLs?

Willy Duitt
07-21-2005, 05:28 AM
What are you saying with these URLs?

Are you asking if URL's speak for themselves??

medigerati
07-21-2005, 05:42 AM
I'm guessing he's saying take a look at the tutorials and see if you can find the problem through there since no one else can seem to find the problem... I wish i could help but I'm not too good with regexp's and the replace function...

dumpfi
07-21-2005, 08:26 AM
function storyDiv(inp) {
inp = inp.replace('\n', '');
mid = inp.replace(/<story\s+class="(h|n|s)"\s+title="(.*?)">(.*?)<\/story>/, '<div class="story$1" xml:lang="en-US" title="$2">\n<h3 class="title">$2</h3>\n<div class="storyData">$3</div>\n</div>');
out = mid.replace(/<story\s+title="(.*?)"\s+class="(h|n|s)">(.*?)<\/story>/, '<div class="story$2" xml:lang="en-US" title="$1">\n<h3 class="title">$2</h3>\n<div class="storyData">$3</div>\n</div>');
return out;
}
alert(storyDiv('<story class="h" title="Cows Have Fun">\n<p>I daresay!</p></story>'));dumpfi

Kenji Miyamoto
07-21-2005, 08:46 AM
That worked, dumpfi, but is there a way I can keep the newlines?

Kenji Miyamoto
07-21-2005, 08:07 PM
Okay, the fix is to use the newline:function storyDiv(inp) {
mid = inp.replace(/<story\s+class=\"(h|n|s)\"\s+title=\"(.*?)\">((\n|.)*?)<\/story>/, "<div class=\"story\$1\" xml:lang=\"en-US\" title=\"\$2\">\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

out = mid.replace(/<story\s+title=\"(.*?)\"\s+class=\"(h|n|s)\">((\n|.)*?)<\/story>/, "<div class=\"story\$2\" xml:lang=\"en-US\" title=\"\$1\">\n <h3 class=\"title\">\$2</h3>\n <div class=\"storyData\">\$3</div>\n </div>");

return out;
}

Kenji Miyamoto
07-23-2005, 02:14 AM
I'm having a bit of trouble with another case: mid = mid.replace(longN[ccc], "<span class=\"hide\">" + replN[ccc] + "</span>");
mid = mid.replace(shortN[ccc], "<span class=\"hide\">" + replN[ccc] + "</span>");Both of the above functions only replace one occurrance of a matched string in only one case. The string is "Eri", and is at the end of the array.