PDA

View Full Version : Does this computer see mpos2? if not, why?



thenextbesthang
10-07-2006, 09:44 PM
var input = window.prompt("Enter Text Here","see this: [[Image:wiki-en.png|Wikipedia]]");
var mpos1 = input.indexOf("[[Image");

for(i=mpos1;i<=input.length;i++)
{
var mpos2 = input.indexOf(":");
}

var istr1 = input.substring(mpos1, mpos2);

Twey
10-07-2006, 09:56 PM
Hm. I'd do:
var input = window.prompt("Enter Text Here","see this: [[Image:wiki-en.png|Wikipedia]]"),
text = input.match(/[[Image:([^\|]+)|([^\]]+)]]/),
url = text[0];
text = text[1];

thenextbesthang
10-07-2006, 11:11 PM
<script type= "text/javascript">
var input = window.prompt("Enter Text Here","see this: [[Image:wiki-en.png|Wikipedia]]");
var mpos1 = input.indexOf("[[Image:");
var init = "[[Image:"
var colon = init.lastIndexOf(":")

for(i=mpos1;i<=input.length;i++)
{
var mpos2 = input.indexOf("|");
}
for(i=mpos1;i<=input.length;i++)
{
var mpos3 = input.indexOf("]]");
}
var istr0 = input.substring(0, mpos1)
var istr1 = input.substring(mpos1, colon);
var websi = input.substring(colon, mpos2)
var istr2 = input.substring(mpos2, mpos2+1);
var istr3 = input.substring(mpos2+1, mpos3);
var istr4 = input.substring(mpos3, mpos3+2);
var istr5 = input.substring(mpos3+2, input.length);

if(istr1.indexOf("[[Image:") != -1)
{
var ipos1 = istr1.replace("[[Image:", '<img src="http://en.wikipedia.org/wiki/Image:');
}

if(istr2.indexOf("|") != -1)
{
var ipos2 = istr2.replace("|", '" alt="');
}
if(istr4.indexOf("]]") != -1)
{
var ipos3 = istr4.replace("]]", '</img>"');
}
var newimage = (istr0+ipos1+websi+ipos2+istr3+ipos3+istr5);
document.write(newimage);
----

when I output this, it doesn't output ipos1.
It doesn't even transform it.

I'm thinkin it has to do with the fact that i can't find mpos2.

Twey
10-08-2006, 11:51 AM
This is really very ugly. Regex is much nicer.
function unWikifyImages(str) {
return str.replace(/\[\[Image:([^\|]+)\|([^\|]+)\]\]/, '<img src="$1" alt="$2">');
}