
Searching for substrings and characters within strings is a common task in programming. Here are some techniques in different languages:
1let str = "Hello World";
2
3// indexOf to find substring
4let index = str.indexOf("World");
5
6// includes to check if substring exists
7let hasHello = str.includes("Hello");
Regular expressions provide powerful pattern matching capabilities. Here is a regex example to match email addresses:
1const pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
2const match = pattern.test("john@email.com");
Wildcards like * and ? can be used for glob style pattern matching:
1const files = fs.readdirSync('./*.js'); // matches all .js files
This covers some basic techniques for searching, pattern matching and globbing in different languages. Regular expressions are a powerful tool for advanced string operations.
xxxxxxxxxx
let str = "Hello World";
let index = str.indexOf("World"); // indexOf to find substring
let hasHello = str.includes("Hello"); // includes to check if substring exists
const pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
const match = pattern.test("john@email.com"); // regex example to match email addresses
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment