
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Wildcard matching of string JavaScript
We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false.
Let's write the code for this function −
Example
const str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => { let count = 0; for(let i = 0; i < first.length; i++){ if(!second.includes(first[i])){ count++; }; if(count > num){ return false; }; }; return true; }; console.log(wildcardMatching(str1, str2, 2)); console.log(wildcardMatching(str1, str2, 1)); console.log(wildcardMatching(str1, str2, 0));
Output
The output in the console will be −
true true false
- Related Articles
- Wildcard Pattern Matching
- Wildcard Matching in Python
- Matching strings with a wildcard in C#
- Forming and matching strings of an array based on a random string in JavaScript
- C++ Program to Perform String Matching Using String Library
- Regular Expression Matching in JavaScript
- Counting matching substrings in JavaScript
- How to perform string matching in MySQL?
- Matching strings for similar characters - JavaScript
- Add matching object values in JavaScript
- Add values of matching keys in array of objects - JavaScript
- C++ Program to Implement String Matching Using Vectors
- Count the Number of matching characters in a pair of Java string
- Group matching element in array in JavaScript
- JavaScript filter an array of strings, matching case insensitive substring?

Advertisements