RegExp array problem

I

iannorton

Hi,

I'm trying to write a RegExp that will return search an array and
return the closest 10 matches, for example, if i entered "Hel" and my
array contained "Hello", "Hell", it would return Hello and Hell,
however, if i entered "ell" it wouldn't return anything.

The code that i need to modify is: -

var searchterm = "Hel";
var re = new RegExp("\\^")
re = /\^/

if(searchterm !='')
{
var matches = 0;
for(var i=0; i<myarray.length; i++)
{
if(searchme != false)
{
if (searchterm == myarray)
{
write (myarray);
matches++;
}

}
if(matches==10) break
}
}

Obviously this is only returning exact matches and having tried to add
the RegExp into there hasn't worked as yet.

Any pointers or tips on this would be great.

Thanks in advance,

Ian
 
T

Thomas 'PointedEars' Lahn

[...]
I'm trying to write a RegExp that will return search an array and
return the closest 10 matches, for example, if i entered "Hel" and
my array contained "Hello", "Hell", it would return Hello and Hell,
however, if i entered "ell" it wouldn't return anything.
[...]
var searchterm = "Hel";
var re = new RegExp("\\^")
re = /\^/

One of these lines is redundant and both deserve a trailing semicolon.
[...]
Obviously this is only returning exact matches and having
tried to add the RegExp into there hasn't worked as yet.

You should have posted your attempts.
Any pointers or tips on this would be great.

// quickhack (untested)

function regExp_escape(s)
{
return s.replace(/[].\\{}[]/g, "\\$1");
}

var re = new RegExp("\\^" + regExp_escape(searchterm))

Up to JavaScript 1.5, JScript .NET:

var filteredArray = new Array();
for (var i = 0, len = myarray.length;
i < len && filteredArray.length < 10;
i++)
{
var o = myarray;

if (re.test(o))
{
filteredArray[filteredArray.length] = o;
}
}

JavaScript 1.6 as implemented in Mozilla/5.0 rv:1.8, incl. Firefox/1.5:

var
i = 0,
filteredArray = myarray.filter(
function(value, index, array)
{
var result;
(result = (i < 10) && re.test(value)) && i++;
return result;
});

That is, however, not the closest matches, it is returning the items with
matching prefix. For the closest matches you will have to implement the
Levenshtein Distance algorithm, for example.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
var re = new RegExp("\\^")
re = /\^/

One of these lines is redundant and both deserve a trailing semicolon.
[...]
var re = new RegExp("\\^" + regExp_escape(searchterm))

D'oh! :)

var re = new RegExp("\\^" + regExp_escape(searchterm));
[...]
JavaScript 1.6 as implemented in Mozilla/5.0 rv:1.8, incl. Firefox/1.5:

var
i = 0,
filteredArray = myarray.filter(
function(value, index, array)
{
var result;
(result = (i < 10) && re.test(value)) && i++;
return result;
});

Or

var
filteredArray = myarray.filter(
function(value, index, array)
{
return re.test(value);
});

for (var i = 10; i--;)
{
// ...
}


PointedEars
 
I

iannorton

I've tried your example as written and played around with it yet it
doesn't appear to be happening for me.

The first problem was that it was choking on,

return s.replace(/[].\\{}[]/g, "\\$1");

which i changed to return s.replace("/[].\\{}[]/g, \\$1");

However, i get the impression that this isn't right,

I'm currently being returned the last entry in the array and nothing
else.

Code below: -

var searchterm = "Hel";

function regExp_escape(s)
{
return s.replace("/[].\\{}[]/g, \\$1");
}

var re = new RegExp("\\^" + regExp_escape(searchterm));



var filteredArray = new Array();
for (var i = 0, len = myarray.length;
i < len && filteredArray.length < 10;
i++)
{
var o = myarray;


if (re.test(o))
{

filteredArray[filteredArray.length] = o;

}
}
write (o);

Thanks for the help,

Ian
 
T

Thomas 'PointedEars' Lahn

I've tried your example as written and played around with it yet it
doesn't appear to be happening for me.

The first problem was that it was choking on,

return s.replace(/[].\\{}[]/g, "\\$1");

I am sorry, it is easy to mix syntax and behavior when you do
different languages at a time. (But I said it was a quickhack :))

a) The rules "] must not be escaped in ranges when used at first
position" and "[" must not be escaped in ranges when used at
last position" apparently do not apply to ECMAScript RegExp.

b) Not the entire match is returned by the $1 backreference, but
only the one in the first pair of parens.
which i changed to return s.replace("/[].\\{}[]/g, \\$1");

However, i get the impression that this isn't right,

Indeed. What was previously two arguments, one RegExp and one
String, are now one String argument. That is not going to work :)

The following works for me:

function regExp_escape(s)
{
return s.replace(/([\].\\{}\[])/g, "\\$1");
}

You may add more special characters to the range when necessary.
Be sure to escape them there where necessary :)
I'm currently being returned the last entry in the array and nothing
else.
[...]
var filteredArray = new Array();
for (var i = 0, len = myarray.length;
i < len && filteredArray.length < 10;
i++)
{
var o = myarray;
if (re.test(o))
{
filteredArray[filteredArray.length] = o;
}
}


The loop should work OK.
write (o);

But you write either the wrong value at the right point or the right value
at the wrong point :)

You could move the document.write() (use the fully qualified call) into
the loop; it is currently outside of it -- note the braces. But I do not
recommend that, and concatenation would not make much sense either.

Therefore, I suggest you join the result and write that string at the
current position, say

document.write(filteredArray.join("<br>\n"));

instead.


HTH

PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 15 Dec
2005 18:12:49 local, seen in Thomas
'PointedEars' Lahn said:
I am sorry, it is easy to mix syntax and behavior when you do
different languages at a time. (But I said it was a quickhack :))

Don't post untested code. It wastes everybody's time.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn said:
I am sorry, it is easy to mix syntax and behavior when you do
different languages at a time. (But I said it was a quickhack :))

Don't post untested code. It wastes everybody's time.

I disagree. This is not a support forum where such would be acceptable.
Testing each and every snippet of code posted here in every possible
environment would be a waste of time as that is impractical. Posting
untested or little tested code allows the user or another poster to find a
potential error, correct it and all that are able and willing to learn will
learn from that; last, but not least, it allows the poster of such code to
attend to other postings. Besides, the snippet I posted was clearly marked
as untested, so anybody that wants to be sure it works without testing, is
free to ignore it.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 12/17/2005 1:50 PM:
Dr John Stockton wrote:

[...] Thomas 'PointedEars' Lahn said:
I am sorry, it is easy to mix syntax and behavior when you do
different languages at a time. (But I said it was a quickhack :))

Don't post untested code. It wastes everybody's time.


I disagree.

You are wrong.
This is not a support forum where such would be acceptable.

That what is acceptable? That you at least test your code in the
browsers you have available? So, you are saying that no one here should
test code at all? Thats plain Stupid.
Testing each and every snippet of code posted here in every possible
environment would be a waste of time as that is impractical.

But at least testing it in 1 or 2 browsers will at least clear syntax
errors.
Posting untested or little tested code allows the user or another poster to find a
potential error, correct it and all that are able and willing to learn will
learn from that; last, but not least, it allows the poster of such code to
attend to other postings.

More stupidity. Do you actually read your arguments before you post them?

Besides, the snippet I posted was clearly marked as untested, so anybody that
wants to be sure it works without testing, is free to ignore it.

And as untested it is worth exactly what you tested it for - nothing. So
why waste the "precious bandwidth" you like to whine so much about to
post crap code?
 

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
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top