Home >Web Front-end >JS Tutorial >How to create a regular expression that only accepts special formulas?

How to create a regular expression that only accepts special formulas?

WBOY
WBOYforward
2023-09-16 19:33:031230browse

How to create a regular expression that only accepts special formulas?

Regular expressions are patterns that contain various characters. We can use regular expressions to search if a string contains a specific pattern.

Here, we will learn to create regular expressions to verify various mathematical formulas. We will use the test() or match() method to check if a specific mathematical formula matches a regular expression

grammar

Users can create regular expressions that accept special mathematical formulas according to the following syntax.

let regex = /^\d+([-+]\d+)*$/g; 

The regular expression above only accepts 10 – 13 12 23, just like a mathematical formula.

Regular expression explanation

  • / / It represents the beginning and end of the regular expression.

  • ^ It represents the beginning of the formula string.

  • \d It represents at least one or more digits at the beginning of the formula.

  • [- ] – Represents the " " and "-" operators in regular expressions.

  • ([- ]\d )* means that the formula can contain numbers, followed by multiple " " or "-" operators.

  • $ It represents the end of the string.

  • g – It matches all occurrences of the identifier.

Example

In the example below, we create a regular expression that accepts formulas that contain numbers " " or the "-" operator.

Users can observe that the first formula matches the regular expression pattern in the output. The second formula does not match the regular expression pattern because it contains the "*" operator. Also, the third formula is the same as the first, but it contains spaces between the operator and the number, so it doesn't match the regular expression.

<html>
<body>
   <h3>Creating the regular expression to validate special mathematical formula in JavaScript</h3>
   <div id = "output"></div>
   <script>
      let output = document.getElementById('output');
      function matchFormula(formula) {
         let regex = /^\d+([-+]\d+)*$/g;
         let isMatch = regex.test(formula);
         if (isMatch) {
            output.innerHTML += "The " + formula + " is matching with " + regex + "<br>";
         } else {
            output.innerHTML += "The " + formula + " is not matching with " + regex + "<br>";
         }
      }
      let formula = "10+20-30-50";
      matchFormula(formula);
      matchFormula("60*70*80");
      matchFormula("10 + 20 - 30 - 50")
   </script>
</body>
</html> 

Regular expressions used in the examples below

We used the /^\d (\s*[- */]\s*\d )*$/g regular expression in the example below. Users can find below an explanation of the regular expressions used.

  • ^\d It represents at least one digit at the beginning of the formula.

  • \s* It represents zero or more spaces.

  • (\s*[- */]\s*\d )* means that the formula can contain spaces and multiple times in the same order. operators, spaces and numbers.

Example

In the following example, we call the TestMultiplyFormula() function three times by passing various formulas as parameters. We use the test() method to check if the formula matches the regular expression pattern.

In the output, we can see that the regular expression accepts formulas with "*" and "/" operators and spaces.

<html>
<body>
   <h2>Creating the regular expression <i> to validate special mathematical formula </i> in JavaScript.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function TestMultiplyFormula(formula) {
         let regex = /^\d+(\s*[-+*/]\s*\d+)*$/g;
         let isMatch = regex.test(formula);
         if (isMatch) {
            output.innerHTML += "The " + formula + " is matching with " + regex + "<br>";
         } else {
            output.innerHTML += "The " + formula + " is not matching with " + regex + "<br>";
         }
      } 
      let formula = "12312323+454+ 565 - 09 * 23";
      TestMultiplyFormula(formula);
      TestMultiplyFormula("41*14* 90 *80* 70 + 90");
      TestMultiplyFormula("41*14& 90 ^80* 70 + 90");
   </script>
</body>
</html>

This tutorial teaches us to create a regular expression that accepts a special mathematical formula. In both examples, we used the test() method to match a formula against a regular expression. Additionally, we used different regular expression patterns in the two examples.

The above is the detailed content of How to create a regular expression that only accepts special formulas?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete