-1

I seem to have confused a lot of people with this question so let me state what I want to do as simply as I can.

I want to search a text string for words that begin with "mak", "mind" and "mass" and end with either nothing extra or "e" or "er". That would be "mak", "make", "maker", "mind", "minde", "minder", "mass", "masse", "masser".

I am trying to match certain words in a text if they start with specific letters and end with specific letters. I am using the following regex:

aray = ['mak','mind', 'mass'];
for(i=0; i < aray.length; i++){
    searchTerm = new RegExp(
        "\\b" + aray[i] + "\\b|" +
        "\\b" + aray[i] + "en\\b|" +
        "\\b" + aray[i] + "er\\b");
    word = testText.match(searchTerm, "gi");
}

The problem is that when the first instance is matched the other instances are not searched for. Could someone point me in the right direction. Any help would be greatly appreciated.

This question has been flagged as a duplicate but the other question does not answer the points I am having difficulties with.

user2343618
  • 165
  • 4
  • share some sample inputs. – Braj Jul 13 '14 at 12:56
  • you should see this topic: http://stackoverflow.com/questions/15090829/javascript-regex-pattern-match-multiple-strings-and-or-against-single-strin – Vladimir Trifonov Jul 13 '14 at 12:56
  • 2
    You have put your `"gi"` flag on the wrong function; it should be in the RegExp constructor. Besides that, I don't see a particular problem here as long as you're using `word` *inside* the loop (outside the loop it will always be whatever the last match was) – Dave Jul 13 '14 at 13:02
  • I thought that the "gi" had to be in the .match() so that the matching continues after the first match. I have tried putting it in the last line of searchTerm but that doesn't work Where exactly are you suggesting it should be? I am using word inside the loop – user2343618 Jul 13 '14 at 13:22
  • "*the terms are anded together anyway*" - how? You seem to *concatenate* them together. – Bergi Jul 13 '14 at 13:28
  • I meant that the elements of any regular expressions are logical “and” by default. Every sequential character in a regular expression is “and’ed” together. Why have you marked my question as a duplicate when it isn't – user2343618 Jul 13 '14 at 13:46
  • You're going to have to explain better why this isn't a duplicate. It still looks like one to me. – Veedrac Jul 13 '14 at 14:06
  • @Veedrac It's not a dupe. OP asked about AND but he meant OR. – Boann Jul 13 '14 at 14:11
  • @Boann Ah. That's not very clear. If you have both the time and will, it might be effective to edit the question to say what it's actually trying to ask. – Veedrac Jul 13 '14 at 14:14
  • @Veedrac I gave it a go. I hope that OP will clarify it though because I'm not certain if I preserved the exact intention. – Boann Jul 13 '14 at 14:27
  • I want to search a text string for words that begin with "mak", "mind" and "mass" and end with either nothing extra or "e" or "er". That would be "mak", "make", "maker", "mind", "minde", "minder", "mass", "masse", "masser". – user2343618 Jul 13 '14 at 16:54
  • 2
    Perhaps what the OP wants is `myRegex = /\b(word1|word2|word3)(en|er)?\b/gi` (note that I'm also using JavaScript's regex literal syntax to simplify escaping and flags) – Dave Jul 13 '14 at 17:02
  • @Boann: we're not always so bad. – David Thomas Jul 13 '14 at 17:29

2 Answers2

0

You are recreating the regular expression every time you do searchTerm = new RegExp(...);, so it will only ever match the last word, unless you're using the expression within the loop, which you claim you are, but it doesn't look like it. I'm still only guessing what you want, but if you want to construct a single regular expression that matches all of the words, you'll need to put all the words in one expression joined with |.

Also, the flags like gi must be passed to the RegExp constructor, not the match method.

var array = ['mak', 'mind', 'mass'];
var searchTerm = new RegExp('\\b(' + array.join('|') + ')(e|er)?\\b', 'gi');
var match = testText.match(searchTerm);
Boann
  • 48,794
  • 16
  • 117
  • 146
0
searchTerm = new RegExp("\\b(?:" + aray[i] + ")(?:er|e)?\\b");
The Regex is: \b(?:mak)(?:er|e)?\b
It consists of:
\b... Word Boundary
Two non-capturing groups (?:) ==> Basically a group which cannot be addressed inside the regex.
The first non-capturing group matches the beginning of the word. (?:mak) ==> mak is a placeholder for an entry of the array.
The second non-capturing group matches the ending of the word. (?:er|e)?
    The pipe character is an OR.
    So this part matches er or e.
    It is necessary to write er before e ==> if switched the regex engine would not find words ending with er.
    The ? at the end means find zero or exact one times and enables finding the beginning without an (er or e ) too.
Andie2302
  • 4,825
  • 4
  • 24
  • 43