[This is the 13th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.]

JavaScript is now so powerful that you can do so many things with it, mobile apps, websites, web apps, games, and even include artificial intelligence. The JavaScript ecosystem has many script libraries and frameworks that you can use to do a lot of things.

On top of that, JavaScript gets some very useful new features added every year, and thanks to the ECMAScript specification, there are now many ways to work with the different data types in JavaScript.

In this article, you’ll look at some common uses for writing regular expressions in JavaScript.

1. match()

Match () is used with strings to check for a match between a string and a regular expression regex, taking the regular expression as an argument.

Grammar:

str.match(regex);
Copy the code

Method returns three possible values:

  • If the regular expression contains agTag, which is a global match, returns an array of all matches without capturing group information;
  • If the regular expression does notgTag, which returns an array containing the first match and its associated capture group;
  • If there is no match at all, it returnsnull

Groups: An object with a named capture group whose key is the name and whose value is the capture group, or undefined if no named capture group is defined.

Example code with tag G:

const strText = "Hello China"; const regex = /[A-Z]/g; Console. log(strtext.match (regex)); // [ 'H', 'C' ]Copy the code

Example code with no tag G:

const text = 'Hello World';
const regex = /[A-Z]/; //Capital letters regex.
console.log(text.match(regex)); // [ 'H', index: 0, input: 'Hello China', groups: undefined ]
Copy the code

When there is no matching instance code:

const strText = "hello china"; const regex = /[A-Z]/; Console. log(strtext.match (regex)); // nullCopy the code

2. test()

Test () tests for a match between a specified string and a regular expression, takes a string as an argument, and returns true or false depending on whether it matches.

Suppose the following string strText is tested for the presence of the word China. You can create a regular expression for finding a word and test for a match between the regular expression and the string strText.

const strText = "hello china";
const regex = /china/;
console.log(regex.test(strText)); // true
Copy the code

Here is the instance code with no match:

const strText = "hello China";
const regex = /china/;
console.log(regex.test(strText)); // false
Copy the code

If you want to ignore the case, you need to use the tag I, as shown in the above code:

const strText = "hello China";
const regex = /china/i;
console.log(regex.test(strText)); // true
Copy the code

Note that syntactically.match() is the “opposite” of.test() in usage

3. search()

The search() method is a string method that can be used with regular expressions. You can pass a regular expression to it as an argument to search the string for a match.

Method returns the position (index) of the first match in the entire string, or -1 if there is no match.

Matching instances:

Const strText = "hello China, I love China "; const regex = /china/; console.log(strText.search(regex)); / / 6Copy the code

No matching instance:

Const strText = "hello China, I love China "; const regex = /devpoint/; console.log(strText.search(regex)); / / 1Copy the code

4. replace()

Replace () searches the string for a specified value or regular expression and replaces it with another value. The method takes two arguments:

  1. The value to search for
  2. The new value to replace

Method returns a new string containing the replaced string. Note that it does not change the original string and only replaces the first value found.

Example code:

const strText = "hello world,i love world";
const regex = /world/;
console.log(strText.replace(regex, "china")); // hello china,i love world
Copy the code

5. replaceAll()

ReplaceAll () is similar to the method replace(), but it allows all matching values or regular expressions in a string to be replaced.

It takes two arguments:

  1. The value to search for, if regular, must carry the global tagg
  2. The new value to replace

It returns a new string with all the new values, again without changing the original string.

Example code:

const strText = "hello world,i love world";
const regex = /world/g;
console.log(strText.replaceAll(regex, "china")); // hello china,i love china
Copy the code

Equivalent to the following code:

const strText = "hello world,i love world";
console.log(strText.replaceAll("world", "china")); // hello china,i love china
Copy the code

By adding the global tag G to the regular expression, you can also replace all the strings that match the regular expression, as follows:

const strText = "hello world,i love world";
const regex = /world/g;
console.log(strText.replace(regex, "china")); // hello china,i love china
Copy the code

conclusion

This article describes common methods that can be used with regular expressions, common and useful in projects, such as form authentication, password authentication, and so on.