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 FormulaLet'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)); Outputtrue 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 FormulaIf 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)); Outputtrue 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)); Outputtrue 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 StringIf 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)); Outputtrue 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 ExpressionsEscaping 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. Comment More infoAdvertise with us Next Article JavaScript - How to Create Regular Expression Only Accept Special Formula? nikhilkalburgi Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2022 JavaScript-Questions +1 More Similar Reads JavaScript - How to Validate Form Using Regular Expression? To validate a form in JavaScript, you can use Regular Expressions (RegExp) to ensure that user input follows the correct format. In this article, we'll explore how to validate common form fields such as email, phone number, and password using RegExp patterns.1. Validating an Email AddressOne of the 4 min read JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read Convert user input string into regular expression using JavaScript In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object 2 min read JavaScript Regular Expressions (RegExp) Examples Regular Expressions in JavaScript provide robust pattern-matching capabilities that are useful for validating input, searching and replacing text, and more. Here, we will explore various examples demonstrating the utility of RegExp in JavaScript.1. Match a WordThe pattern /hello/ matches the string 3 min read How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu 3 min read JavaScript RegExp [^abc] Expression The [^abc] expression in JavaScript regular expressions is a negated character set. It matches any character except those specified inside the brackets. For example, [^abc] matches any character that is not a, b, or c.JavaScriptlet regex = /[^abc]/g; let str = "abcdefg"; let matches = str.match(rege 2 min read How to set a regular expression for an input element value that checked against using HTML ? In this article, we will use <input> pattern attribute to set the regular expression pattern for an input field that checked against. The HTML <input> pattern attribute is used to specify the regular expression on which the input elements value is checked against. This attribute works wi 1 min read How to build a Math Expression Tokenizer using JavaScript ? A math expression tokenizer is a fundamental component in parsing mathematical expressions. It breaks down a mathematical expression into smaller units called tokens, which are easier to process and evaluate. In JavaScript, building a math expression tokenizer can be achieved through various approac 2 min read How to Determine if a value is Regular Expression using Lodash? When working with dynamic data in JavaScript, it's often necessary to validate the types of values, including checking whether a value is a regular expression (RegExp). Lodash provides an elegant and easy-to-use utility function for this: _.isRegExp. This function allows us to verify if a given valu 2 min read JavaScript RegExp (Regular Expression) A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It's often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).I 4 min read Like