Regular expressions or RegExp can be used in JavaScript to search for patterns in texts. Searching for a certain character in a string, such as locating all occurrences of a word character, is a typical activity. In this article, we’ll look at how to utilize RegExp in JavaScript to find word characters in a string.
Terminologies:
- Regular expressions: These are basically patterns that are used to match character combinations in strings.
- Character classes: These are basically a group of characters contained in square brackets that can match any of the enclosed characters.
- Word characters: A word character in RegEx is any letter, numeric, or underscore character. The shorthand character class “w” represents it.
Modifiers:
- /g modifier: Indicates to perform a global search.
- /m modifier: Indicates to match the pattern on each line of the string.
- /i modifier: Indicates to perform a case-insensitive search.
Different approaches to finding word characters in a string:
Approach 1: Using the test() method: The simplest way to find word characters in a string is to use the test() method of a regular expression. It returns true if the pattern matches the string and if not then false.
Javascript
const str = "Welcome to GeekforGeeks!" ; const regex = /\w/; // Matches any word character if (regex.test(str)) { console.log( "Found a word character" ); } else { console.log( "No word characters found" ); } |
Output:
Found a word character
Approach 2: Using the match() method: Another approach is to use the match() method of a string, which returns an array of all the matches found in the string.
Javascript
const str = "Welcome to GeekforGeeks!" ; const regex = /\w/g; // Matches all word characters const matches = str.match(regex); if (matches) { console.log(`Found ${matches.length} word characters`); console.log(matches); } else { console.log( "No word characters found" ); } |
Output:
Found 21 word characters [ 'W', 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o', 'G', 'e', 'e', 'k', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ]
Approach 3: Using the exec() method: A third approach is to use the exec() method of a regular expression, which returns an array of the first match found in the string. You can then use a loop to find all the matches.
Javascript
const str = "GeekforGeeks" ; const regex = /\w/g; // Matches all word characters let match; while ((match = regex.exec(str)) !== null ) { console.log(`Found word character "${match[0]}" at index ${match.index}`); } |
Output:
Found word character "G" at index 0 Found word character "e" at index 1 Found word character "e" at index 2 Found word character "k" at index 3 Found word character "f" at index 4 Found word character "o" at index 5 Found word character "r" at index 6 Found word character "G" at index 7 Found word character "e" at index 8 Found word character "e" at index 9 Found word character "k" at index 10 Found word character "s" at index 11
Examples that are using different modifiers:
Using the /g modifier: In this example, we can perform a full global search to find word characters in a string is to use the test() method of a regular expression.It returns true if the pattern matches the string and if not then false.
Javascript
const str = "Welcome to GeekforGeeks!" ; const regex = /\w/; // Matches any word character console.log(regex.test(str)); |
Output:
true
Using the /m modifier: This is similar to the previous modifier, the only difference is that it performs a search on each line of the string. The example below returns true if the pattern matches the string and if not then false.
Javascript
const str = 'Welcome to\nGeekforGeeks\nWebsite' ; const regex = /GeekforGeeks/m; console.log(regex.test(str)); |
Output:
true
Using the /i modifier: This modifier is used when we have to perform a case-insensitive search. This example will return true if the pattern matches the string and if not then false.
Javascript
const str = 'GeekforGeeks is a computer science portal for neveropen' ; const regex = /geekforneveropen/i; console.log(regex.test(str)); |
Output:
true