JavaScript Regexp Dynamic Generation from Variables

Javascript Regexp dynamic generation from variables?

You have to use RegExp:

str.match(new RegExp(pattern1+'|'+pattern2, 'gi'));

When I'm concatenating strings, all slashes are gone.

If you have a backslash in your pattern to escape a special regex character, (like \(), you have to use two backslashes in the string (because \ is the escape character in a string): new RegExp('\\(') would be the same as /\(/.

So your patterns have to become:

var pattern1 = ':\\(|:=\\(|:-\\(';
var pattern2 = ':\\(|:=\\(|:-\\(|:\\(|:=\\(|:-\\(';

JS Regex dynamic generation from variables

Create the regular expression pattern in a variable and then use new RegExp() with that variable that has the pattern. And don't use g as a part of the pattern, pass it as a second argument of RegExp() instead.

var string = "How are you doing today? You are such a nice person!";var text = 'are';var regEx = '\\b'+text+'\\b';var count =  string.match(new RegExp(regEx,"g")).length;alert(count);

Use dynamic (variable) string as regex pattern in JavaScript

To create the regex from a string, you have to use JavaScript's RegExp object.

If you also want to match/replace more than one time, then you must add the g (global match) flag. Here's an example:

var stringToGoIntoTheRegex = "abc";
var regex = new RegExp("#" + stringToGoIntoTheRegex + "#", "g");
// at this point, the line above is the same as: var regex = /#abc#/g;

var input = "Hello this is #abc# some #abc# stuff.";
var output = input.replace(regex, "!!");
alert(output); // Hello this is !! some !! stuff.

JSFiddle demo here.

In the general case, escape the string before using as regex:

Not every string is a valid regex, though: there are some speciall characters, like ( or [. To work around this issue, simply escape the string before turning it into a regex. A utility function for that goes in the sample below:

function escapeRegExp(stringToGoIntoTheRegex) {
return stringToGoIntoTheRegex.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

var stringToGoIntoTheRegex = escapeRegExp("abc"); // this is the only change from above
var regex = new RegExp("#" + stringToGoIntoTheRegex + "#", "g");
// at this point, the line above is the same as: var regex = /#abc#/g;

var input = "Hello this is #abc# some #abc# stuff.";
var output = input.replace(regex, "!!");
alert(output); // Hello this is !! some !! stuff.

JSFiddle demo here.


Note: the regex in the question uses the s modifier, which didn't exist at the time of the question, but does exist -- a s (dotall) flag/modifier in JavaScript -- today.

Dynamic construction of RegEx based on user input

When input comes from a user, it's a good idea to escape the string using the following:

RegExp.escape = function (s)
{
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

You call it like this:

 let patern =  new RegExp('\\b' + RegExp.escape(this.input));

That way it Works even when the input has special characters in it, like '\' or parenthesis-

Dynamic making Regexp

you can pass the string (value) in the RegExp constructor, along with the ignoreCase flag as:

 var value = 'bal';
var b = new RegExp(value, 'i')
b.test('BAL')

it returns true.

How do you use a variable in a regular expression?

Instead of using the /regex\d/g syntax, you can construct a new RegExp object:

var replace = "regex\\d";
var re = new RegExp(replace,"g");

You can dynamically create regex objects this way. Then you will do:

"mystring1".replace(re, "newstring");

Javascript RegExp: Create regex with variable

Like so:

new RegExp(q + " Gonzalez", "i");

Using the / characters is how to define a RegExp with RegExp literal syntax. To create a RegExp from a string, pass the string to the RegExp constructor. These are equivalent:

var expr = /Josh Gonzalez/i;
var expr = new RegExp("Josh Gonzalez", "i");

The way you have it you are passing a regular expression to the regular expression constructor... it's redundant.

How to use dynamic variable between regular expression

Use this.

var regex="(.)*?01-02-2016(.)*?\\|\\#\\|";var rx=new RegExp(regex,"igm");console.log(rx);//Then when do you want to change,regex=regex.replace("01-02-2016","03-02-2016");rx=new RegExp(regex,"igm");console.log(rx);

Dynamic Regular Expression Generation in JavaScript

You should use the RegExp without /.../ to allow the use of variables in a regular expression, e.g.:

var specialCharactersValidation = new RegExp("^[a-zA-Z0-9]{" + characterCount + "}$");

And double-escape special characters inside.

From MDN:

The literal notation provides compilation of the regular expression
when the expression is evaluated. Use literal notation when the
regular expression will remain constant. For example, if you use
literal notation to construct a regular expression used in a loop, the
regular expression won't be recompiled on each iteration.

How to create value for RegEx dyanmically?

new RegExp(value, 'i');

RegExp documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp



Related Topics



Leave a reply



Submit