How to use String search() With a Regular Expression In Javascript
Use a regular expression to do a case-insensitive search for “w3wiki” in a string:
Example:
function myFunction() {
// input string
let str = "Visit w3wiki!";
// searching string with modifier i
let n = str.search(/w3wiki/i);
console.log(n);
// searching string without modifier i
let n = str.search(/w3wiki/);
console.log(n);
}
myFunction();
Output:
6
-1
JavaScript RegExp(Regular Expression)
A regular expression is a character sequence defining a search pattern. It’s employed in text searches and replacements, describing what to search for within a text. Ranging from single characters to complex patterns, regular expressions enable various text operations with versatility and precision.
A regular expression can be a single character or a more complicated pattern.
Syntax:
/pattern/modifiers;
Example:
let patt = /w3wiki/i;
Explanation :
/w3wiki/i is a regular expression.
w3wiki is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching:
Expressions | Descriptions |
---|---|
g | Find the character globally |
i | Find a character with case-insensitive matching |
m | Find multiline matching |
Regular Expression Brackets can Find characters in a specified range
Expressions | Description |
---|---|
[abc] | Find any of the characters inside the brackets |
[^abc] | Find any character, not inside the brackets |
[0-9] | Find any of the digits between the brackets 0 to 9 |
[^0-9] | Find any digit not in between the brackets |
(x | y) | Find any of the alternatives between x or y separated with | |
Regular Expression Metacharacters are characters with a special meaning:
Metacharacter | Description |
---|---|
\. | Search single characters, except line terminator or newline. |
\w | Find the word character i.e. characters from a to z, A to Z, 0 to 9 |
\d | Find a digit |
\D | Search non-digit characters i.e all the characters except digits |
\s | Find a whitespace character |
\S | Find the non-whitespace characters. |
\b | Find a match at the beginning or at the end of a word |
\B | Find a match that is not present at the beginning or end of a word. |
\0 | Find the NULL character. |
\n | Find the newline character. |
\f | Find the form feed character |
\r | Find the carriage return character |
\t | Find the tab character |
\v | Find the vertical tab character |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxxx |
Regular Expression Quantifiers are used to define quantitiesoccurrence
Quantifier | Description |
---|---|
n+ | Match any string that contains at least one n |
n* | Match any string that contains zero or more occurrences of n |
n? | Match any string that contains zero or one occurrence of n |
m{X} | Find the match of any string that contains a sequence of m, X times |
m{X, Y} | Find the match of any string that contains a sequence of m, X to Y times |
m{X,} | Find the match of any string that contains a sequence of m, at least X times |
m$ | Find the match of any string which contains m at the end of it |
^m | Find the match of any string which contains m at the beginning of it |
?!m | Find the match of any string which is not followed by a specific string m. |
Regular Expression Object Properties:
Property | Description |
---|---|
constructor | Return the function that created the RegExp object’s prototype |
global | Specify whether the “g” modifier is set or not |
ignorecase | Specify whether the “i” modifier is set or not |
lastindex | Specify the index at which to start the next match |
multiline | Specify whether the “m” modifier is set or not |
source | Return the text of RegExp pattern |
Regular Expression Object Methods:
Method | Description |
---|---|
compile() | Used to compile the regular expression while executing of script |
exec() | Used to test for the match in a string. |
test() | Used to test for a match in a string |
toString() | Return the string value of the regular expression |
Below is an example of the JavaScript Regular Expressions.
Example:
function GFGFun() {
let str = "Visit w3wiki";
let n = str.search(/w3wiki/i);
console.log(n);
}
GFGFun();
Output:
6