views:

179

answers:

2

I need a form with one button and window for input

that will check an array, via a regular expression.

And will find a exact match of letters + numbers. Example wxyz [some space btw] 0960000

or a mix of numbers and letters [some space btw] + numbers 01xg [some space btw] 0960000

The array has four objects for now.

Once found i need a function the will open a new page or window when match is found .

Thanks you for your help.

Michael

+2  A: 

To answer the Javascript part, here's one way to "grep" through the array to find matching elements:

var matches = [];
var re = /whatever/;

foo.forEach(
  function(el) {
    if( re.exec(el) )
      matches.push(el);
  }
);


To attempt to answer the regular expression part: I don't know what "exact match" means to you, and I'm assuming "some space" belongs only in between the other terms, and I'm assuming letters means the English alphabet from 'a' to 'z' in lower and upper case and the digits should be 0-9 (otherwise, other language characters might be matched).

The first pattern would be /[a-zA-Z0-9]+\s*0960000/. Change "\s*" to "\s+" if there is at least one space, instead of zero or more space characters. Change "\s" to " " if matching the tab character (and some lesser-used space chars) is not desirable.

For the second pattern, I don't know what "numbers 01xg" means, but if it means numbers followed by that string, then the pattern would be /[a-zA-Z0-9]+\s*[0-9]+\s*01xg\s*0960000/. The same caveats apply as above.

Additionally, this will match a partial string. If the string much be matched in entirety (if nothing in the string must exist except that which is matched), add "^" to the beginning of the pattern to anchor it to the beginning of the string, and "$" at the end to anchor it to the end of the string. For example, /[a-zA-Z0-9]+\s*0960000/ matches "foo_bar 5 0960000", but /^[a-zA-Z0-9]+\s*0960000$/ does not.

For more on regular expressions in Javascript, take a look at developer.mozilla.org's article on the RegExp object (the link takes you to JS version 1.5 reference, which should apply to all JS-capable browsers).

(edited to add): To match either situation, since they have overlapping parts, you could use the following pattern: /[a-zA-Z0-9]+(?:\s*[0-9]+\s*01xg)?\s*0960000/. The question mark says to match the part that differs -- in a non-matching group (?:foo) -- once or zero times. (?:foo)? and (?:foo|) do the same thing in this case, but I'm not sure whether there is a performance difference; I would recommend to use the one that makes the most sense to you, so you can read it later.

Anonymous
A: 

Still no resolution , but thanks to all who attempted to help.

Thank you,

Michael

don't post comments as answers. You may put them in your question body or in a comment for an answer (like this...)
eKek0
Why do you have 2 accounts?
Darryl Hein