473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

RegExp array problem

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[i])
{
write (myarray[i]);
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

Dec 15 '05 #1
7 2542
ia*******@gmail.com wrote:
[...]
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[i];

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
Dec 15 '05 #2
Thomas 'PointedEars' Lahn wrote:
ia*******@gmail.com wrote:
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
Dec 15 '05 #3
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[i];
if (re.test(o))
{

filteredArray[filteredArray.length] = o;

}
}
write (o);

Thanks for the help,

Ian

Dec 15 '05 #4
ia*******@gmail.com wrote:
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[i];
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
Dec 15 '05 #5
JRS: In article <21****************@PointedEars.de>, dated Thu, 15 Dec
2005 18:12:49 local, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :

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.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Dec 16 '05 #6
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn <Po*********@web.de> posted :
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
Dec 17 '05 #7
Thomas 'PointedEars' Lahn said the following on 12/17/2005 1:50 PM:
Dr John Stockton wrote:

[...] Thomas 'PointedEars' Lahn <Po*********@web.de> posted :
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?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: denisb | last post by:
hello all, for a search engine, I'm looking for a regexp than can do the job it is just (!) a 's' problem (frequent in french) : I have a field 'thesaurus' in a table MySQL (3.23.56) ; I...
5
by: Gordan | last post by:
Hi I have an array with following values: 419; 20; 19;20;21; 18;30; 17;20;25; 16;26; 16;17;20;21;22;34
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
3
by: jgarrard | last post by:
Hi, I have an array of strings which are regular expressions in the PERL syntax (ie / / delimeters). I wish to create a RegExp in order to do some useful work, but am stuck for a way of...
27
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from...
3
by: Paddy | last post by:
Lets say i have a generator running that generates successive characters of a 'string' characters then I would have to 'freeze' the generator and pass the characters so far to re.search. It is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.