I want regular expression which return true if any continuous three charters match.
For e.g /[money]{3,}/g
It return true for mon, one, ney and return false for mny.
 
    
    - 85,780
- 21
- 159
- 179
 
    
    - 129
- 1
- 5
- 
                    and what is the problem? – winner_joiner Mar 22 '17 at 06:52
- 
                    Why can't you do this `/(?:mon|one|ney)/g` ? – abhishekkannojia Mar 22 '17 at 06:54
- 
                    3Using a regex here is pointless (and a mess if you want it to be dynamic), use indexOf('mon') !== -1 instead. – ryanlutgen Mar 22 '17 at 06:57
- 
                    2and what have you tried so far? – Sagar V Mar 22 '17 at 07:00
- 
                    If live validation is meant: `/(m(?:on?)?|o(?:ne?)?|n(?:ey?)?/`. Anyway, the question is way too unclear. – Wiktor Stribiżew Mar 22 '17 at 07:54
2 Answers
I would not use regex, why not use indexOf, it's less code and better to read.
something like "money".indexOf("mon")>-1
Here a Demo, with all listed examples:
let values = ["mon","one", "ney", "mny"];
let shouldMatch = "money";
for (let idx = 0; idx<values.length;idx++){
    console.info(values[idx], "=", shouldMatch.indexOf(values[idx])>-1);
}But If you want to use RegExp, you could use it like this:
(BTW: this is only a "fancy" way to write the example above)
let values = ["mon","one", "ney", "mny"];
function matcher(word, value){
    return (new RegExp(value)).test(word);
}
for (let idx = 0; idx<values.length;idx++){
    console.info(values[idx], "=", matcher("money", values[idx]));
}The Code Basically:
Creates a new Regular Expression exp.
(new RegExp("mon"))(equal to/mon/) and than just testing, if the "pattern" matches the word"money"(new RegExp("mon")).test("money")this returnstrue.Here it is all turned around, we are checking if money fits into the (sub)-pattern mon.
 
    
    - 1
- 1
 
    
    - 12,173
- 4
- 36
- 61
Regular expressions function as a search for a character string, your application would require taking a base string and dynamically building an insane regex with many ORs and lookaheads/behinds.  For your application, write a function that uses indexOf
function stringContainsSubstr(sourceStr, subStr) {
  return sourceStr.indexOf(subStr) !== -1;
}
var exampleStrs = ["mon", "one", "ney", "mny"];
var str = "money";
for (var i = 0; i < exampleStrs.length; i++) {
  console.log(stringContainsSubstr(str, exampleStrs[i]));
}
 
    
    - 2,951
- 1
- 21
- 31