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.
Check whether a string matches a regex in JS
How to use String.match() in javascript - Stack Overflow
Help please, JS regex: match everything after a string
Switch case statement with string matching for whole case
I'm not sure you understand how switch statements work.
switch(someVariable) {
case 'foo':
doSomething()
break
case 'bar':
doSomethingElse()
break
default:
break
}
The switch(someVariable) gets evaluated once. So if the value of someVariable was 'foo' then the first case would be run, if it was 'bar' then the second case. If it was something else, then it would fall back to the default case.
In your example, your switch statement is evaluating item. It does a comparison to the first case, which is a method invocation that returns a value (true/false), the method is invoked and the value that the switch is comparing to is either true or false. Since the value of item is probably not a boolean, then the case is false and it does not run the code block for it.
From the looks of it, you're better off using a simple if/else.
const jQuerySplit = data.data.split('.')
jQuerySplit.forEach((item, i) => {
if (item.match(/^\$\("[a-zA-z0-9]"\)$/g)) {
console.log('it works')
} else {
console.log('failed')
}
})
More on reddit.com Videos
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");
}
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);