How to concatenate regex literals in JavaScript ? Last Updated : 15 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Regex is a sequence of pattern that is used for matching with a pattern. While searching for data in a text, the search pattern is described for what we are searching for. It can be a single character or a more complex pattern. It can be used to perform all types of text searches. Regex has its own static and instance properties. Syntax: /pattern/modifiers Example: A regular expression. /gfg/g Where, gfg is a pattern (to be used in a search).g is a modifier (modifies the search to be case-insensitive). The concatenation of Regex in the programming world can be understood as combining text patterns to obtain a new text pattern, such as "Hello" + "World" is /HelloWorld/. Whenever RegExp() is called, it creates a new RegExp object. Example 1: This example creating an expression without actually using the Regex literal syntax. This allows you to make arbitrary string manipulation before it becomes a Regex object. JavaScript function gfg() { var segment_part = " GeeksforGeeks |" + " A computer science portal for geeks"; var pattern = new RegExp("GFG:" + /*comment here */ segment_part + /* that was defined just now */ "is a computer science portal"); console.log(pattern); } gfg(); Output: /GFG: GeeksforGeeks | A computer science portal for geeksis a computer science portal/ Example 2: If you have two Regex literals, you can concatenate them using a technique where it removes duplicates, but keep the unique values in order, joining both the regex literals. Example: /hello/y + / world/g would be /hello world/gy JavaScript function gfg() { var regex1 = /geeks/g; var regex2 = / for geeks/y; var flags = (regex1.flags + regex2.flags).split("") .sort().join("") .replace(/(.)(?=.*\1)/g, ""); var regex3 = new RegExp(regex1.source + regex2.source, flags); console.log(regex3); } gfg(); Output: /geeks for geeks/gy Comment More infoAdvertise with us Next Article How to concatenate regex literals in JavaScript ? M mohitgouti Follow Improve Article Tags : JavaScript Web Technologies HTML JavaScript-Questions Similar Reads How to Concatenate Strings in JavaScript? Here are the various methods to concatenate strings in JavaScript1. Using Template Literals (Template Strings)Template literals (introduced in ES6) provide a simple way to concatenate strings. With template literals, you can embed expressions inside a string using ${} syntax. This method makes the c 3 min read How to create multi-line strings in JavaScript ? In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo 3 min read How to break JavaScript Code into several lines ? In this article, we will discuss how to break javascript code into several lines. We generally break the code into several lines so that it can be read easily by a third person or the programmer itself. There are a few ways to break JavaScript code into several lines: We can use the backslash \ char 1 min read How to modify a string in JavaScript ? JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. Representing a String: 1. Using double 3 min read How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var 2 min read How to Search a String for a Pattern in JavaScript ? Here are two ways to search a string for a pattern in JavaScript.1. Using the string search() MethodThe JavaScript string search() method is used to search a specified substring within the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the p 2 min read 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 Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read Create a string with multiple spaces in JavaScript We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript. Below 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 Like