How to Concatenate Regex Literals in JavaScript

How can I concatenate regex literals in JavaScript?

Here is how to create a regular expression without using the regular expression literal syntax. This lets you do arbitary string manipulation before it becomes a regular expression object:

var segment_part = "some bit of the regexp";
var pattern = new RegExp("some regex segment" + /*comment here */
segment_part + /* that was defined just now */
"another segment");

If you have two regular expression literals, you can in fact concatenate them using this technique:

var regex1 = /foo/g;
var regex2 = /bar/y;
var flags = (regex1.flags + regex2.flags).split("").sort().join("").replace(/(.)(?=.*\1)/g, "");
var regex3 = new RegExp(expression_one.source + expression_two.source, flags);
// regex3 is now /foobar/gy

It's just more wordy than just having expression one and two being literal strings instead of literal regular expressions.

How to concatenate regular expressions in JavaScript?

You don't have to use the literal notation. You could instead create a new RegExp object.

var myRegex = new RegExp("^animal: (rami|shmulik|dudu)$");

JavaScript regex pattern concatenate with variable

var re = new RegExp("/\b"+test+"\b/"); 

\b in a string literal is a backspace character. When putting a regex in a string literal you need one more round of escaping:

var re = new RegExp("\\b"+test+"\\b"); 

(You also don't need the // in this context.)

How to concatenate strings for building a regex?

You need to double escape instead of single escape that is you have to use 2 slashes.

Also you don't have to add / at the start and end in RegExp function.

const pattern = new RegExp(`/^${patternOne}${patternTwo}$/`, 'i');
^^ ^^ -> remove those

It will be added automatically

const patternOne = '(\\w+:{0,1}\\w*@)?';const patternTwo = '([a-zA-Z0-9.-]+)';
const pattern = new RegExp("^" + patternOne + patternTwo + "$", 'i');console.log(pattern);

Combining regular expressions in Javascript

The answer is yes! You have to initialize the variable under the RegExp class:

var lower = new RegExp(/--RegexCode--/);
var upper = new RegExp(/--RegexCode--/);

hence, regex can be dynamically created. After creation:

"sampleString".replace(/--whatever it should do--/);

Then you can combine them normally, yes.

var finalRe = new RegExp(lower.source + "|" + upper.source);

Template literal inside of the RegEx

Your regex variable is a String. To make it a RegExp, use a RegExp constructor:

const regex = new RegExp(String.raw`pattern_as_in_regex_literal_without_delimiters`)

For example, a regex literal like /<\d+>/g can be re-written as

const re = RegExp(String.raw`<\d+>`, 'g') // One \ is a literal backslash
const re = RegExp(`<\\d+>`, 'g') // Two \ are required in a non-raw string literal

To insert a variable you may use

const digits = String.raw`\d+`;
const re = RegExp(`<${digits}>`, 'g')

To solve your issue, you may use

const regex = new RegExp(`.+?(?=${elemvalue.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i"); 

Also, it is a good idea to escape the variable part in the regex so as all special regex metacharacters were treated as literals.

const s = "final (location)";
const elemvalue = "(location)";
const regex = new RegExp(`.+?(?=${elemvalue.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i");
// console.log(regex); // /.+?(?=\(location\))/i
// console.log(typeof(regex)); // object
let a = s.replace(regex, '');
console.log(a);


Related Topics



Leave a reply



Submit