Home > front end >  How can filter string that don't match in totally?
How can filter string that don't match in totally?

Time:12-16

I want to filter string that not have all coincidences (I deleted blank spaces)

With string.includes()
'videocardgigabytegeforce3070'.includes('videocardgigabyte') return true
'videocardgigabytegeforce3070'.includes('videocardgeforce') return false

I want to second case also return true, If you have a solution with function or regex I'll appreciate it

CodePudding user response:

const str = 'videocardgigabytegeforce3070';

const regex = /videocard.*geforce/;
const result = regex.test(str);

console.log(result); // true

or

const str = 'videocardgigabytegeforce3070';

const regex = /videocard.*geforce/;
const result = str.match(regex);

if (result && result.length > 0) {
  console.log(true); // true
} else {
  console.log(false);
}

CodePudding user response:

To get the behavior you described, you can use a regular expression with the test method.

Here's an example using a regular expression that will match the string "videocard" followed by either "gigabyte" or "geforce":

const string = 'videocardgigabytegeforce3070';
const regex = /videocard(gigabyte|geforce)/;
console.log(regex.test(string));  // true

The regular expression /videocard(gigabyte|geforce)/ uses a group with the | character, called a "pipe," to match either "gigabyte" or "geforce." The test method returns true if the regular expression matches any part of the string, and false if it doesn't.

  • Related