Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to create regular expression only accept special formula ?

How to create regular expression only accept special formula ?

In this article, you will understand the use of regular expressions to accept special formulas. Here we will start by understanding to use Regular Expressions in Javascript and later we will understand to create the pattern for the special formulas. Regular expressions are patterns used to match character combinations in strings.

Example 1: This expression accepts the words and spaces.

\w+\s

Example 2: This expression accepts the digits of length 1 or more.

\d+

More about Regular expressions here.

We can construct a regular expression in one of two ways:

  • Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:
const re = /a+b/;
  • Or calling the constructor function of the RegExp object, as follows:
const re = new RegExp('a+b');

Regular Expression for special formula: The special formula consists of operands and operators.

Syntax:

formula = operand operator operand

where,

  • operand = [0-9]
  • operator = [ – + * % ^ / ]

General Regular Expression for this looks like this:

digit (operand digit)*

where,

  • digit = [0-9]
  • operand = [ – + * % ^ / ]
  • x* = 0 or more instances

We will make use of Javascript RegExp, character classes, and quantifiers to specify the formula. Let’s first understand them in brief.

1. Javascript RegExp: This is the data type in javascript used to create Regular expressions.

Syntax:

var RE = /pattern/flags

where,

  • the pattern is the expression to match
  • flags are g = global case , i = case-insensitive , m = multiline etc.

2. Quantifier: It indicates the number of characters to match.

Syntax:

n+ = one or more
n* = zero or more

3. Character Classes: These indicate kinds of characters like digits or letters.

Syntax:

\d : digit
\D : non-digit
\w : alphabet
\W : non-alphabet

Steps to create regular expression only accept the special formula

  • Create a new instance of a regular expression with a pattern.
  • Add the pattern using character classes and quantifiers.
  • Test using test( ) method implementation in Javascript.

To accept a formula let’s say for example 4-3+2+1, you can use an expression ^\d+([+-]\d+)*$ in Javascript.

where,

  • ^: Matches the beginning of input.
  • \d: digit.
  • x+: Matches the preceding item “x” 1 or more times.
  • $: Matches the ending of input.
  • [ ]: set of valid characters, The valid characters are the operators like plus, minus, etc.

Example 1: Let us implement the expression 4-3+2+1 in javascript using the test() method.

Javascript




<script>
const re = /^\d+([+-]\d+)*$/g;
console.log(re.test("4-3+2+1"));
</script>


Output:

true

Example 2: Let us implement the expression 4*5-3/2 in javascript using the test() method by including the * and / operators.

Javascript




<script>
const re = /^\d+(?:[-+*/^]\d+)*$/g;
console.log(re.test("4*5-3/2"));
</script>


Output:

true

Example 3: Invalid match for example 1

Javascript




<script>
const re = /^\d+([+-]\d+)*$/g;
console.log(re.test("4-3*1/2"));
</script>


Output:

false

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments