Convert user input string into regular expression using JavaScript Last Updated : 28 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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 Regular expressions (RegExp) are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. There are two ways to construct a regular expression in JavaScript. Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows. const reg = /ab+/; Calling the constructor function of the RegExp object, as follows. const reg = new RegExp('ab+', flag); Using the constructor function provides run time compilation of the regular expression, hence we should use the second method here as the string is a dynamic input from the user. Both the above expressions correspond to the same RegExp. Example 1: For the string, we will take "Geeks For Geeks'". Input : '^Ge' Output: ["Ge"] 0: "Ge" Input : '[A-z]+' Output: (3) ["Geeks", "For", "Geeks"] 0: "Geeks" 1: "For" 2: "Geeks" JavaScript const str = "Geeks for Geeks"; // Input from User const regex = prompt("Enter RegExp"); // Conversion from string to RegExp const reg = new RegExp(regex, "g"); // The match fn returns the array of strings // That match to RegExp const result = str.match(reg); if (result) console.log(result); else console.log("Not Found"); Output: Example 2: In this example, we will use the RegExp constructor. JavaScript const userInput = prompt("Enter a regular expression pattern:"); const regex = new RegExp(userInput); console.log(regex); Output: Comment More infoAdvertise with us Next Article Convert user input string into regular expression using JavaScript K ksucheta19 Follow Improve Article Tags : JavaScript Web Technologies javascript-string regular-expression JavaScript-Questions +1 More Similar Reads How to clone a given regular expression in JavaScript ? In this article, we will know How to clone a regular expression using JavaScript. We can clone a given regular expression using the constructor RegExp(). The syntax of using this constructor has been defined as follows:- Syntax: new RegExp(regExp , flags) Here regExp is the expression to be cloned a 2 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 How to convert a string into kebab case using JavaScript ? Given a string with space-separated or camel case or snake case letters, the task is to find the kebab case of the following string. Examples: Input: Geeks For GeeksOutput: geeks-for-geeksInput: GeeksForGeeksOutput: geeks-for-geeksInput: Geeks_for_geeksOutput: geeks-for-geeksBelow are the approaches 3 min read 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 How to return all matching strings against a regular expression in JavaScript ? In this article, we will learn how to identify if a string matches with a regular expression and subsequently return all the matching strings in JavaScript. We can use the JavaScript string.search() method to search for a match between a regular expression in a given string. Syntax: let index = stri 3 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to trim a file extension from string using JavaScript ? Given a fileName in string format and the task is to trim the file extension from the string using JavaScript. replace() method: This method searches a string for a defined value or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newv 4 min read How to Validate Email Address without using Regular Expression in JavaScript ? Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server.An email address must have the follo 5 min read JavaScript - How to Create Regular Expression Only Accept Special Formula? 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 lik 3 min read Replace special characters in a string with underscore (_) in JavaScript We will explore several methods to replace special characters in a string with an underscore (_) in JavaScript. Special characters are non-alphanumeric symbols, and replacing them is a common requirement in various use cases such as sanitizing input or formatting file names.Table of ContentJavaScrip 2 min read Like