Open In App

JavaScript - How to Create Regular Expression Only Accept Special Formula?

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To ensure that a string matches a specific formula or pattern, you can use a regular expression (RegExp) in JavaScript. Here are the various ways to create a regular expression that only accepts regular formulas.

1: Alphanumeric Code Formula

Let's create a regular expression that matches a formula like an alphanumeric code of exactly 6 characters: 3 letters followed by 3 digits (e.g., ABC123).

JavaScript
let regex = /^[A-Za-z]{3}\d{3}$/;

let code1 = "ABC123";
let code2 = "AB1234";
let code3 = "123ABC";

console.log(regex.test(code1));
console.log(regex.test(code2)); 
console.log(regex.test(code3));

Output
true
false
false
  • ^[A-Za-z]{3}: Matches exactly 3 letters (case-insensitive).
  • \d{3}$: Matches exactly 3 digits.
  • ^ and $: Ensure that the entire string matches the pattern from start to finish.

2. Complex Mathematical Formula

If you want to create a regular expression that matches simple mathematical formulas like a+b=c, where a, b, and c are digits, and the operator is +, you can use:

JavaScript
let regex = /^\d+\+\d+=\d+$/;

let a1 = "2+3=5";
let a2 = "10+20=30";
let a3 = "2+3=6";

console.log(regex.test(a1)); 
console.log(regex.test(a2));
console.log(regex.test(a3));

Output
true
true
true
  • ^\d+: Matches one or more digits at the beginning of the string.
  • \+: Matches the literal plus symbol. Note that + is a special character in regular expressions, so it must be escaped with a backslash (\).
  • \d+: Matches one or more digits.
  • =: Matches the literal equals symbol.
  • \d+$: Matches one or more digits at the end of the string.

3. Date Format (YYYY-MM-DD)

If your special formula is a date format (YYYY-MM-DD), you can use a regular expression to validate it:

JavaScript
let regex = /^\d{4}-\d{2}-\d{2}$/;

let date1 = "2024-12-03";
let date2 = "2024-13-03";
let date3 = "24-12-03";

console.log(regex.test(date1));
console.log(regex.test(date2)); 
console.log(regex.test(date3)); 
  • ^\d{4}: Matches exactly 4 digits for the year.
  • -: Matches the literal hyphen.
  • \d{2}: Matches exactly 2 digits for the month and day.
  • $: Ensures the string ends after the date.

4. Phone Number Format (XXX-XXX-XXXX)

For a phone number in the format XXX-XXX-XXXX (e.g., 123-456-7890), the following regular expression can be used

JavaScript
let regex = /^\d{3}-\d{3}-\d{4}$/;

let phone1 = "123-456-7890";
let phone2 = "1234567890";
let phone3 = "123-45-67890";

console.log(regex.test(phone1));
console.log(regex.test(phone2)); 
console.log(regex.test(phone3));

Output
true
false
false
  • ^\d{3}: Matches exactly 3 digits for the area code.
  • -: Matches the literal hyphen.
  • \d{3}: Matches exactly 3 digits for the first part of the phone number.
  • \d{4}$: Matches exactly 4 digits for the second part of the phone number.

5. Accepting Only Specific Symbols in a String

If your formula involves accepting only certain special symbols, like only +, -, and * (for a mathematical expression), the following pattern can be used:

JavaScript
let regex = /^[\+\-\*]+$/;

let expr1 = "+-+*";
let expr2 = "++**";
let expr3 = "abc";

console.log(regex.test(expr1));
console.log(regex.test(expr2));
console.log(regex.test(expr3)); 

Output
true
true
false
  • ^[\+\-\*]+$: The square brackets [] define a character set that matches only +, -, and * symbols.
  • +: The + inside the character set means one or more of the allowed characters.
  • ^ and $: Ensure the entire string only contains the specified characters.

Tips for Creating a Special Formula with Regular Expressions

  • Escaping Special Characters: When using symbols like +, ., *, or ? in regular expressions, make sure to escape them with a backslash (\) since they have special meanings.
  • Use Grouping and Quantifiers: Regular expressions support grouping with parentheses () and quantifiers like {n} (exact number of occurrences), + (one or more), and * (zero or more).
  • Test the Formula: Always test your regular expression with various sample inputs to ensure that it correctly matches the intended formula and rejects invalid inputs.

Next Article

Similar Reads