You can use a string as a regular expression using the RegExp Object:
var myPattern = new RegExp('.'+myWord,'g');
Fiddle Example
Doing a single match in your case, is simply changed the second parameter in RegExp from g to m (which means to make one match per line for multi lines, but in this case a strings is simply all one line). For finding the word "wood" from "ood","od","d", or other cases. You can do this:
var myPattern = new RegExp("\\b\\w+"+myWord+"\\b",'m');
Note I have a solution in the comments below, but this one is better.
The items \\b ... \\b mean word boundaries. Basically ensuring that it matches a single word. Then the \\w means any valid "word character". So overall the regexp means (using myWord = "od"):
|word boundary| + (1 or more word characters) + "od" + |word boundary|
This will ensure that it matches any words in the string than end with the characters "od". Or in a more general case you can do:
var myPattern = new RegExp("\\b\\w*"+myWord+"\\w*\\b",'m');
Answer from Spencer Wieczorek on Stack OverflowVideos
You can use a string as a regular expression using the RegExp Object:
var myPattern = new RegExp('.'+myWord,'g');
Fiddle Example
Doing a single match in your case, is simply changed the second parameter in RegExp from g to m (which means to make one match per line for multi lines, but in this case a strings is simply all one line). For finding the word "wood" from "ood","od","d", or other cases. You can do this:
var myPattern = new RegExp("\\b\\w+"+myWord+"\\b",'m');
Note I have a solution in the comments below, but this one is better.
The items \\b ... \\b mean word boundaries. Basically ensuring that it matches a single word. Then the \\w means any valid "word character". So overall the regexp means (using myWord = "od"):
|word boundary| + (1 or more word characters) + "od" + |word boundary|
This will ensure that it matches any words in the string than end with the characters "od". Or in a more general case you can do:
var myPattern = new RegExp("\\b\\w*"+myWord+"\\w*\\b",'m');
Create a Regexp object like
new RegExp('.ood','g');
like in
var searchstring='ood' // this is the one you get in a variable ...
var myString = "How much wood could a wood chuck chuck";
var myPattern=new RegExp('.'+searchstring,'gi');
var myResult = myString.match(myPattern);
Use regex.test() if all you want is a boolean result:
console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false
console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true
console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true
...and you could remove the () from your regexp since you've no need for a capture.
Use test() method :
var term = "sample1";
var re = new RegExp("^([a-z0-9]{5,})$");
if (re.test(term)) {
console.log("Valid");
} else {
console.log("Invalid");
}