Javascript Concatenator - has bugs

S

Sean Kinsey

Hi all,
I've produced a string concatenator for a specific purpose, but it has a
bug.http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_....

It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...

any ideas on how to catch and kill this bug?

cheers.

Does this do the trick?

var input ="My-Video.wmv\nMy-Video2.wmv\n";
var reLine = /^([-\w]+)(\.\w+)$/m;
var output = input.replace(reLine,"<li><a onclick=
\"play(this.href);return false\" href=\"$1$2\">$1</a></li>");
 
1

123Jim

I've produced a string concatenator for a specific purpose, but it has a
Does this do the trick?
var input ="My-Video.wmv\nMy-Video2.wmv\n";
var reLine = /^([-\w]+)(\.\w+)$/m;
var output = input.replace(reLine,"<li><a onclick=
\"play(this.href);return false\" href=\"$1$2\">$1</a></li>");

clever stuff! .. That regex is alien to me ;)
It does work the same in firefox and IE ... but so far I only have it
returning one line for the first list item, and the unchanged remaining list
items.

what is that $1$2 stuff? looks like perl variables..
How does it 'know' those are the names in the list?

and how can I have it process all the list items?

I guess understanding your regex would explain all ..
know of a good online resource?
 
Ø

Øyvind Sean Kinsey

Den 11.04.2010 18:41, skrev 123Jim:
Does this do the trick?
var input ="My-Video.wmv\nMy-Video2.wmv\n";
var reLine = /^([-\w]+)(\.\w+)$/m;
var output = input.replace(reLine,"<li><a onclick=
\"play(this.href);return false\" href=\"$1$2\">$1</a></li>");

clever stuff! .. That regex is alien to me ;)
It does work the same in firefox and IE ... but so far I only have it
returning one line for the first list item, and the unchanged remaining list
items.

Add the global flag to the regexp :/^([-\w]+)(\.\w+)$/mg
^
what is that $1$2 stuff? looks like perl variables.

Thats references to the two matched groups, grouped by the parentices ()
How does it 'know' those are the names in the list?

Not sure what you mean
and how can I have it process all the list items?

See above
I guess understanding your regex would explain all ..
know of a good online resource?

A good tip is to actually try to find the information your self before
asking others about it. Hint: google it.
 
1

123Jim

Øyvind Sean Kinsey said:
Den 11.04.2010 18:41, skrev 123Jim:
Does this do the trick?
var input ="My-Video.wmv\nMy-Video2.wmv\n";
var reLine = /^([-\w]+)(\.\w+)$/m;
var output = input.replace(reLine,"<li><a onclick=
\"play(this.href);return false\" href=\"$1$2\">$1</a></li>");

clever stuff! .. That regex is alien to me ;)
It does work the same in firefox and IE ... but so far I only have it
returning one line for the first list item, and the unchanged remaining
list
items.

Add the global flag to the regexp :/^([-\w]+)(\.\w+)$/mg
^
what is that $1$2 stuff? looks like perl variables.

Thats references to the two matched groups, grouped by the parentices ()
How does it 'know' those are the names in the list?

Not sure what you mean
and how can I have it process all the list items?

See above
I guess understanding your regex would explain all ..
know of a good online resource?

A good tip is to actually try to find the information your self before
asking others about it. Hint: google it.


Thanks, got it working now ..
Of course my first recourse was to web search for stuff on regex, but
everyone in the world has a favourite resource on regex right?

One of the things I have learned here is that regex can simplify/save many
lines of code in specific cases. .. I just gotta learn to use it.
Thanks again.
 
1

123Jim

123Jim said:
Hi all,
I've produced a string concatenator for a specific purpose, but it has a
bug.
http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_on_contents.html

It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...

any ideas on how to catch and kill this bug?

cheers.

Turns out javascript in IE inserts a linefeed character where Firefox and
Chrome do not.
I have modified my javascript to take this into account and now it works as
intended in IE.

I didn't choose to use the regex method as I could not figure out how to
create a regex to match every character which might, but probably won't,
appear in the 'files' list.

Here's how I killed the bug:

var newtext = "";
for(i=0;i<=numberoflines;i++){
if(textarray2.length > 1){
//if no linefeed in textarray2 then
if(textarray2.lastIndexOf("\r")==-1){
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}else{ //for IE - delete the linefeed character
//delete \r linfeed character
textarray2 = textarray2.replace("\r","");
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}
}
}
document.myform.outputtext.value = ""; //clear the text area
document.myform.outputtext.value += newtext;
 
S

Sean Kinsey

Hi all,
I've produced a string concatenator for a specific purpose, but it has a
bug.
http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_...
It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...
any ideas on how to catch and kill this bug?

Turns out javascript in IE inserts a linefeed character where Firefox and
Chrome do not.
I have modified my javascript to take this into account and now it works as
intended in IE.

I didn't choose to use the regex method as I could not figure out how to
create a regex to match every character which might, but probably won't,
appear in the 'files' list.

Here's how I killed the bug:

var newtext = "";
for(i=0;i<=numberoflines;i++){
   if(textarray2.length > 1){
         //if no linefeed in textarray2 then
       if(textarray2.lastIndexOf("\r")==-1){
           newtext = newtext.concat(
               newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
       }else{ //for IE - delete the linefeed character
           //delete \r linfeed character
           textarray2 = textarray2.replace("\r","");
           newtext = newtext.concat(
               newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
       }
    }}

document.myform.outputtext.value = ""; //clear the text area
document.myform.outputtext.value += newtext;


So because you couldn't create a proper regex you have opted on a
subpar and overly complex solution?
I doubt it should take anyone with a decent grip on regex more than a
couple of minutes to create one that works.
\w matches all 'word' caracters (a-z, A-Z, 0-9, _), - matches '-', \/
matches '/' \\ matches '\' and \. matches '.'
and all together: /^([-\w\/\\\.]+)(\.\w+)$/mg where the first group
matches the above, and the second dot followed by 'characters' (the
file extension).

I have not tested the above, but it should be fairly close.
 
1

123Jim

Sean Kinsey said:
Hi all,
I've produced a string concatenator for a specific purpose, but it has
a
bug.
http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_...
It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...
any ideas on how to catch and kill this bug?

Turns out javascript in IE inserts a linefeed character where Firefox and
Chrome do not.
I have modified my javascript to take this into account and now it works
as
intended in IE.

I didn't choose to use the regex method as I could not figure out how to
create a regex to match every character which might, but probably won't,
appear in the 'files' list.

Here's how I killed the bug:

var newtext = "";
for(i=0;i<=numberoflines;i++){
if(textarray2.length > 1){
//if no linefeed in textarray2 then
if(textarray2.lastIndexOf("\r")==-1){
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}else{ //for IE - delete the linefeed character
//delete \r linfeed character
textarray2 = textarray2.replace("\r","");
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}
}}

document.myform.outputtext.value = ""; //clear the text area
document.myform.outputtext.value += newtext;

So because you couldn't create a proper regex you have opted on a
subpar and overly complex solution?
I doubt it should take anyone with a decent grip on regex more than a
couple of minutes to create one that works.
\w matches all 'word' caracters (a-z, A-Z, 0-9, _), - matches '-', \/
matches '/' \\ matches '\' and \. matches '.'
and all together: /^([-\w\/\\\.]+)(\.\w+)$/mg where the first group
matches the above, and the second dot followed by 'characters' (the
file extension).
I have not tested the above, but it should be fairly close.

Thanks for the reply ....I tested your regex , but ,,. what if the filenames
contains spaces, apostrophes, ~ # | ?.... What about the myriad other
characters that may appear in a filename .. I don't intend for my code to
break on some erroneous text list of possible 'filenames'. Also I don't
intend the code should warn that some filenames contain illegal characters
(in whatever platform) . the platforms vary in illegal characters I believe.

I could also modify the page to process non file names .. so there might be
very little restrictions on the characters that should be matched by the
regex.

I'm sure there must be a regex way , but I was also interested to find out
why my code was presenting a bug in IE.

cheers.
 
S

Sean Kinsey

Hi all,
I've produced a string concatenator for a specific purpose, but it has
a
bug.
http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_....
It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...
any ideas on how to catch and kill this bug?
cheers.
Turns out javascript in IE inserts a linefeed character where Firefox and
Chrome do not.
I have modified my javascript to take this into account and now it works
as
intended in IE.
I didn't choose to use the regex method as I could not figure out how to
create a regex to match every character which might, but probably won't,
appear in the 'files' list.
Here's how I killed the bug:
var newtext = "";
for(i=0;i<=numberoflines;i++){
if(textarray2.length > 1){
//if no linefeed in textarray2 then
if(textarray2.lastIndexOf("\r")==-1){
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}else{ //for IE - delete the linefeed character
//delete \r linfeed character
textarray2 = textarray2.replace("\r","");
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}
}}
document.myform.outputtext.value = ""; //clear the text area
document.myform.outputtext.value += newtext;

So because you couldn't create a proper regex you have opted on a
subpar and overly complex solution?
I doubt it should take anyone with a decent grip on regex more than a
couple of minutes to create one that works.
\w matches all 'word' caracters (a-z, A-Z, 0-9, _), - matches '-', \/
matches '/' \\ matches '\' and \. matches '.'
and all together: /^([-\w\/\\\.]+)(\.\w+)$/mg where the first group
matches the above, and the second dot followed by 'characters' (the
file extension).
I have not tested the above, but it should be fairly close.


Thanks for the reply ....I tested your regex , but ,,. what if the filenames
contains spaces, apostrophes, ~ # | ?.... What about the myriad other
characters that may appear in a filename .. I don't intend for my code to
break on some erroneous text list of possible 'filenames'. Also I don't
intend the code should warn that some filenames contain illegal characters
(in whatever platform) . the platforms vary in illegal characters I believe.

I could also modify the page to process non file names .. so there might be
very little restrictions on the characters that should be matched by the
regex.

I'm sure there must be a regex way , but I was also interested to find out
why my code was presenting a bug in IE.

cheers.


A filename with '#' or '?' in it? Are you talking about filenames
(used on a filesystem) or a Uniform Resource Locator (URL)?
Its no problem creating a simple regexp either way, for instance /^(.
+?)(\.\w+)$/mg

This will match all 'filenames' and give you the two groups you need.
 
S

Scott Sauyet

Sean said:
A filename with '#' or '?' in it? Are you talking about filenames
(used on a filesystem) or a Uniform Resource Locator (URL)?

This is a legal file name in Windows XP:

`~!@#$%^&()-_=+[]{};',.txt

The only restrictions I know of are against '\', '/', ':', '*', '?',
'<', '>', and '|'.

-- Scott
 
S

Sean Kinsey

Sean said:
A filename with '#' or '?' in it? Are you talking about filenames
(used on a filesystem) or a Uniform Resource Locator (URL)?

This is a legal file name in Windows XP:

    `~!@#$%^&()-_=+[]{};',.txt

The only restrictions I know of are against '\', '/', ':', '*', '?',
'<', '>', and '|'.

  -- Scott

It was primarily the '?' I was wondering about as it is not allowed on
Windows or on POSIX compliant systems (http://en.wikipedia.org/wiki/
Filename#Comparison_of_file_name_limitations)

Anyhow, the above regexp does the trick.
 
S

Sean Kinsey

Hi all,
I've produced a string concatenator for a specific purpose, but it has
a
bug.
http://myweb.tiscali.co.uk/ladycroft/Rewrite_line_in_text_file_based_....
It's functional in firefox and chrome but in IE an extra newline (wrap)
appears in the output causing a problem. ...
any ideas on how to catch and kill this bug?
cheers.
Turns out javascript in IE inserts a linefeed character where Firefox and
Chrome do not.
I have modified my javascript to take this into account and now it works
as
intended in IE.
I didn't choose to use the regex method as I could not figure out how to
create a regex to match every character which might, but probably won't,
appear in the 'files' list.
Here's how I killed the bug:
var newtext = "";
for(i=0;i<=numberoflines;i++){
if(textarray2.length > 1){
//if no linefeed in textarray2 then
if(textarray2.lastIndexOf("\r")==-1){
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}else{ //for IE - delete the linefeed character
//delete \r linfeed character
textarray2 = textarray2.replace("\r","");
newtext = newtext.concat(
newtext1, textarray2, newtext3, textarray4, newtext5,
"\n");
}
}}
document.myform.outputtext.value = ""; //clear the text area
document.myform.outputtext.value += newtext;

So because you couldn't create a proper regex you have opted on a
subpar and overly complex solution?
I doubt it should take anyone with a decent grip on regex more than a
couple of minutes to create one that works.
\w matches all 'word' caracters (a-z, A-Z, 0-9, _), - matches '-', \/
matches '/' \\ matches '\' and \. matches '.'
and all together: /^([-\w\/\\\.]+)(\.\w+)$/mg where the first group
matches the above, and the second dot followed by 'characters' (the
file extension).
I have not tested the above, but it should be fairly close.


Thanks for the reply ....I tested your regex , but ,,. what if the filenames
contains spaces, apostrophes, ~ # | ?.... What about the myriad other
characters that may appear in a filename .. I don't intend for my code to
break on some erroneous text list of possible 'filenames'. Also I don't
intend the code should warn that some filenames contain illegal characters
(in whatever platform) . the platforms vary in illegal characters I believe.

I could also modify the page to process non file names .. so there might be
very little restrictions on the characters that should be matched by the
regex.

I'm sure there must be a regex way , but I was also interested to find out
why my code was presenting a bug in IE.

cheers.


Your bug was probably due to the difference between \r which I believe
most browsers use in textareas, and \r\n which I belive IE uses.
 
S

Scott Sauyet

Andrew said:
This is a legal file name in Windows XP:
     `~!@#$%^&()-_=+[]{};',.txt
The only restrictions I know of are against '\', '/', ':', '*', '?',
'<','>', and '|'.

Try naming a text file con.txt

Or aux.gif, or lpt1.pdf, or com3.html, or anything else using one of
the DOS reserved words.

True, and there are probably other obscure restrictions as well, but
the set of allowed characters is larger than might be expected.

-- Scott
 
T

Thomas 'PointedEars' Lahn

Scott said:
Andrew said:
Sean Kinsey wrote:
A filename with '#' or '?' in it? Are you talking about filenames
(used on a filesystem) or a Uniform Resource Locator (URL)?
This is a legal file name in Windows XP:
`~!@#$%^&()-_=+[]{};',.txt

The only restrictions I know of are against '\', '/', ':', '*', '?',
'<','>', and '|'.

Try naming a text file con.txt

Or aux.gif, or lpt1.pdf, or com3.html, or anything else using one of
the DOS reserved words. ^^^^^^^^^^^^^^^^^^
True, and there are probably other obscure restrictions as well, but
the set of allowed characters is larger than might be expected.

Pardon?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Scott said:
The point was simply that Windows allows as part of file names
characters that one might not expect.

My question referred to the marked part.


PointedEars
 
S

Scott Sauyet

Thomas said:
      ^^^^^^^^^^^^^^^^^^
Pardon?

from Wikipedia article on filenames [1]:

| In addition, in Windows and DOS, some words might also be reserved
| and can not be used as filenames.[3] For example, DOS Device file:
|
| CON, PRN, AUX, CLOCK$, NUL
| COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
| LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.

-- Scott
____________________
[1] http://en.wikipedia.org/wiki/Filename
 
T

Thomas 'PointedEars' Lahn

Scott said:
Thomas said:
^^^^^^^^^^^^^^^^^^
Pardon?

from Wikipedia article on filenames [1]:

| In addition, in Windows and DOS, some words might also be reserved
| and can not be used as filenames.[3] For example, DOS Device file:
|
| CON, PRN, AUX, CLOCK$, NUL
| COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
| LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.

I was aware of that list, but I find especially the term "reserved word" in
that context misleading. Microsoft does not use it, they use "reserved
device name" instead.

I was not aware that it is apparently not possible to create con.txt etc.
on Windows. Thanks for that. However, I wonder as to the reason. This
might a problem specific to the Windows "Save As" dialog in connection with
its "File Type" field. In DOSEMU I had no problem creating CON3.TXT. I
will test in a real DOS when and if I find the time.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top