Click to See Complete Forum and Search --> : whats wrong with this code?[bb code]
blazes816
08-18-2006, 07:31 PM
Hey. I'm working on parsing bb tags and I can do it in PHP, but translating it to javascript is going a bit rough. What's wrong with this? This just deletes everything from my textarea.
function parsetags(){
var tags = new Array('/\[b\](.*?)\[\/b\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[color\=(.*?)\](.*?)\[\/color\]/is','/\[align\=(.*?)\](.*?)\[\/align\]/is')
var reps = new Array('<b>\\1</b>','<a href="\\1">\\2</a>','<i>\\1</i>','<u>$1</u>','<p style="color: #$1";">\\2</p>','<p align=$1>$2</p>')
if (navigator.appName=="Netscape"){
var letterbody = document.newletter.letterbody;
}
for (x in tags)
{
letterbody.value.replace(/tags[x]/, reps[x]);
}
}
konithomimo
08-18-2006, 07:54 PM
1. there is no if-statement, so you shouldn't use an else-if-statement.
2. don't use predefined values such as "form" for your variable names.
blazes816
08-18-2006, 08:02 PM
1. There was an if that I took out to reduce the size of the code I posted.
2. Okay. Script revised.
konithomimo
08-18-2006, 08:07 PM
Without seeing the entire code it is kinda hard to tell what is wrong.
Kravvitz
08-18-2006, 10:23 PM
1) Why are you doing browser sniffing? There is no need to do that here. That line that is only running in browsers claiming to be Netscape should be run in all browsers.
2) I recommend you use a normal for() loop instead of a for-in loop.
3) Remove the quotes around all of the RegExp's they are part of the problem.
4) Change all of the \\1 to $1 and \\2 to $2.
blazes816
08-18-2006, 10:55 PM
function parsetags(){
var tags = new Array(/\[b\](.*?)\[\/b\]/is,/\[url\=(.*?)\](.*?)\[\/url\]/is,/\[i\](.*?)\[\/i\]/is,/\[u\](.*?)\[\/u\]/is,/\[color\=(.*?)\](.*?)\[\/color\]/is,/\[align\=(.*?)\](.*?)\[\/align\]/is)
var reps = new Array('<b>$1</b>','<a href="$1">$2</a>','<i>$1</i>','<u>$1</u>','<p style="color: #$1";">$2</p>','<p align=$1>$2</p>')
var letterbody = document.newletter.letterbody;
for (var i = 0;i<tags.length;i++) {
letterbody.value.replace(tags[i], reps[i]);
}
}
Okay. But i'm getting an error:
Error: illegal character
Source Code:
var tags = new Array(\/\[b\](.*?)\[\/b\]/is,/\[url\=(.*?)\](.*?)\[\/url\]/is,/\[i\](.*?)\[\/i\]/is,/\[u\](.*?)\[\/u\]/is,/\[color\=(.*?)\](.*?)\[\/color\]/is,/\[align\=(.*?)\](.*?)\[\/align\]/is)
And an arrow pointing to the first '\'. I've tried removing it and it gives me the same error. :confused:
Kravvitz
08-18-2006, 11:05 PM
Remove that and remove the "s" option as well.
What browser are you testing in?
Why do you want a JavaScript version of this code anyway?
blazes816
08-19-2006, 06:42 AM
firefox/IE
I want it in JS, because I read that if you use JS and then write it to the text area, the html will parse.
BetaScriptin
08-19-2006, 02:42 PM
function parsetags(){
var tags = new Array(/\(.*?)/gim,/\[url=(.*?)\](.*?)\[\/url\]/gim,/\[i\](.*?)\[\/i\]/gim,/\[u\](.*?)\[\/u\]/gim,/\[color=(.*?)\](.*?)\[\/color\]/gim,/\[align=(.*?)\](.*?)\[\/align\]/gim)
var reps = new Array('<b>$1</b>','<a href="$1">$2</a>','<i>$1</i>','<u>$1</u>','<p style="color: #$1";">$2</p>','<p align=$1>$2</p>')
var letterbody = document.newletter.letterbody;
for(i=0;i<tags.length;i++) {
for(x=0;x<reps.length;x++) {
letterbody.value.replace(tags[i], reps[x]);
}
}
}
ive never used Regexp with arrays before, but am i wrong in the fact that you need to backslash?
also, use gim as it works much better.
And one more thing, for (var i = 0;i<tags.length;i++) {
the i in the for stament works with the tags array...but why dont you have a for statement for your reps array?
any do you even have to declare i as a variable?
sorry, i may be wrong on these points as ive never really used arrays before...
konithomimo
08-19-2006, 03:58 PM
function parsetags(){
var tags = new Array(/\(.*?)/gim,/\[url=(.*?)\](.*?)\[\/url\]/gim,/\[i\](.*?)\[\/i\]/gim,/\[u\](.*?)\[\/u\]/gim,/\[color=(.*?)\](.*?)\[\/color\]/gim,/\[align=(.*?)\](.*?)\[\/align\]/gim)
var reps = new Array('<b>$1</b>','<a href="$1">$2</a>','<i>$1</i>','<u>$1</u>','<p style="color: #$1";">$2</p>','<p align=$1>$2</p>')
var letterbody = document.newletter.letterbody;
for(i=0;i<tags.length;i++) {
for(x=0;x<reps.length;x++) {
letterbody.value.replace(tags[i], reps[x]);
}
}
}
ive never used Regexp with arrays before, but am i wrong in the fact that you need to backslash?
also, use gim as it works much better.
And one more thing, for (var i = 0;i<tags.length;i++) {
the i in the for stament works with the tags array...but why dont you have a for statement for your reps array?
any do you even have to declare i as a variable?
sorry, i may be wrong on these points as ive never really used arrays before...
1. gim is not necessarily better, only in certain instances.
2. The two arrays correspond, so there is no need for multiple for-loops, since he wants tag[i] to be replaced with its corresponding value in reps[], which is reps[i]
3. You do not need to declare it, but it is good practice because for most OOP languages, and even non-OOP languages such as C++, you have to delcare the variables otherwise you will get an error and your code will not run.
BetaScriptin
08-19-2006, 04:19 PM
1. gim seems to be the right choice in a code like this though, because the values in the arrays being replaced may appear more than once, always change from upper to lower case depending on how it is put in, and they may also span over several lines=P
2. o...i see, thanx for pointing that out=P
3. i usually never declare it, as it saves space, and most of my methods do not call for it being used. also, i will never use OOP or C++ =P but it is a good idea for him to use it, since he may very well be into OOP and C++.
and i was also wondering if i was right about the backslash...do you need them or not when using arrays?
blazes816
08-21-2006, 04:37 PM
Thanks for the help everyone.