| re: detecting '\' in a string
Stephen wrote:[color=blue]
> Hi,
>
> I need to detect the presence of a backslash character in a string and
> escape that character using the replace function. I seem to be able to do
> this with any character except the backslash character. The replace
> function is ignoring the presence of the baclslash. The simple code I am
> using is below:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>
> <html>
> <head>
> <title>Untitled</title>
> <script language="JavaScript1.2">
> function FileName(){
> filenameTest = 'hello \ goodbye'
> alert(filenameTest.replace('\\','\\\\'))
> }
> </script>
> </head>
> <body onload="FileName()">
> </body>
> </html>
>
> Like I say, if I try to replace any other character it is fine but it seems
> not to be able to detect the backslash in the variable filenameTest
>
> Any ideas?
>
> Thanks,
> Stephen[/color]
That's because there's no backslash in it. Your replace is fine, but
your test case is what's flawed: by not escaping the backslash, you've
escaped the space that comes after it. Try this:
filenameTest = 'hello \\ goodbye'
Ironic, isn't it? |