PDA

View Full Version : match() confusion


Graeme Hackston
02-06-2003, 06:31 AM
I can't see what I'm doing wrong here. Why is the function finding matches to characters that aren't in the string?

Also, why can't I include characters like "+" and "(" in the array?


<html>
<head>
<title></title>
<script>

function Check_For_Valid_Path(Path) {
Char = new Array('!','@','&','%','^','#','$','|','{','}')
str = ''
for (var i=0;i<Char.length;i++) {
if (Path.match(Char[i])) {
alert(Path.indexOf(Char[i]))
str += Char[i]
}
}
alert(str)
}

test_str = 'string'

onload = function() {
Check_For_Valid_Path(test_str)
}

</script>
</head>
<body>
</body>
</html>

beetle
02-06-2003, 08:00 AM
Because the argument that is passed to match() is received as a pattern, not a string. Regular expression's meta-characters need to be escaped. The meta characters are

\ | ( ) [ { ^ $ * + ? . < >

As you can see, you've got quite a few of those in your array.

What are you trying to do, anyway?

Graeme Hackston
02-06-2003, 08:29 AM
Thanks beetle, that makes sence.

I'm just fiddling with an hta I made. Part of it is an image browser and I'm trying to put image info into the alt text. When a file name contains invalid characters, I'm getting a "file not found" error when I try to access the file size using fso.GetFile(file).size.

I'm not entirely sure of what characters are invalid, I just noticed it when I tried to load an image with "%" in the name. I did a search and came up with these.

/ ! @ # $ } { % ^ & * ( ) + | , : "

I tried escaping in the array but it didn't work. What can I do to test a path string for invalid file characters?

Graeme Hackston
02-06-2003, 10:23 PM
I think I've got it

Char = new Array('!','@','&','%','^','#','$','|','{','}','+','(',')','*')
str = ''
for (var i=0;i<Char.length;i++) {
foo = '\\\' + Char[i]
if (Path.match(foo)) {

Thanks for your help beetle