SoFunction
Updated on 2025-03-03

JavaScript implementation basics regular expressions

JavaScript supports regular expressions through the RegExp class, to give the simplest example:
Copy the codeThe code is as follows:

var regApple = new RegExp("apple");

It can match the first "apple" string that appears in a string and is case sensitive. Adding the second parameter "g" to the construction method represents searching out all "apple" in the string, where "g" represents "global". If the second parameter is "i", it means case-insensitive, and the case of the letter will not be considered during the matching process. Combining the above two, you can search for all "apple" strings without considering case issues.
Copy the codeThe code is as follows:

var regApple = new RegExp("apple", "gi");

Regular expressions have not unique representation methods. Using the syntax in Perl language, the above expression can be represented as:
Copy the codeThe code is as follows:

var regApple = /apple/gi;

After creating a RegExp object, the RegExp method can construct different matching methods. Because regular expressions are operations on strings, some String methods also play an important role in constructing regular expressions.
Methods of RegExp Objects
Copy the codeThe code is as follows:

var sampleString = "Greenapple";
var regApple = /apple/;
alert((sampleString));

The result output from the above code is "true", because the sampleString contains the string "apple" that needs to be matched, which is the easiest way to detect. Sometimes, we need to know the detailed results of the match, for example:
Copy the codeThe code is as follows:

var sampleString = "green apples, red apples";
var regApple = /apple/g;
var arr = (sampleString);

By using the exec() method, the returned arr is an array of matching results, including each matched value and its segment, such as "green apples" or "red apples" in the above example. The match() method has the same function as exec(), but the expression is different:
Copy the codeThe code is as follows:

var sampleString = "green apples, red apples";
var regApple = /apple/g;
var arr = (regApple);

The search() method is similar to indexOf(), returning the location of the first matching string:
Copy the codeThe code is as follows:

var sampleString = "green apples, red apples";
var regApple = /apple/gi;
alert((regApple)); //Output "6"

String method
String's replace() method can replace the specified string with another string:
Copy the codeThe code is as follows:

var sampleString = "There is a green apple.";
alert(("green", "red")); //Output "There is a red apple."

Replace the first parameter of replace() with a regular expression to achieve the same effect:
Copy the codeThe code is as follows:

var sampleString = "There is a green apple.";
var regApple = /apple/;
alert((regApple, "red")); //Output "There is a red apple."

The second parameter of replace() can be replaced by a function() that accepts a matching string as an argument and returns a replacement string. (There are questions)
Using regular expressions can implement the same function as String's split() method.
Copy the codeThe code is as follows:

var fruit = "apple,pear,lemon";
var arr = (",");

Use regular expressions:
Copy the codeThe code is as follows:

var fruit = "apple,pear,lemon";
var reg = /\,/;
var arr = (reg);