Regex with variables in it [duplicate] - javascript

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
/^([A-Za-z0-9]){1,8}$/
This is a normal way to write a regex in JavaScript but I want to construct the regex dynamically with a variable in between ().
Variable = [A-Za-z0-9]

This is how you can build a new regular expression from string:
var v = '[A-Za-z0-9]';
var regExp = new RegExp('^(' + v + '){1,8}$');
console.log(regExp);
Now you can use the regular expression regExp in your purpose

Related

Add a variable into a regex [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I've got a little problem of comprehension with my regex
I just want add a variable inside my regex
I've got this:
static test(Prefix, version: string) {
return "/^" + Prefix + "[0-9]*.[0-9]*.[0-9]*-[0-9]*-(SNAPSHOT)$/.test(version);
}
// I don't understand where to put my last "
thanks for your help
You can build a regexp expression via new RegExp(string)

Remove both words from string with JavaScript? [duplicate]

This question already has answers here:
Why does javascript replace only first instance when using replace? [duplicate]
(3 answers)
Closed 3 years ago.
I need to remove 2 words from a string. The words are _with and _and so raised_hand_with_fingers_and_splayed becomes raised_hand_fingers_splayed
The regex /_with|_and/ appears to work in https://regexr.com/ but when I use it with JavaScript only the _with is removed:
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/,"")
You need the g modifier to perform multiple replacements. Otherwise it just replaces the first match.
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/g,"")
console.log(newStr);

Is there a way to insert a character into a RegExp pattern? [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 4 years ago.
I am refactoring a series of RegExp a/b patterns that are used in two ways.
One uses the ^ as the third character.
/[^a-zA-Z0-9,?#&!#'"~ _;\.\*\/\-\+ ]/
The other doesn't use the ^
/[a-zA-Z0-9,?#&!#'"~ _;\.\*\/\-\+ ]/
Rather than write this pattern in two way is there a way to write it once and add the ^ to a second version?
This throws an error but as an example is there something available like this?
varOne = /[a-zA-Z0-9,?#&!#'"~ _;\.\*\/\-\+ ]/
varTwo = [varOne.slice(0, 1), '^', varOne.slice(1)].join('')
You need to use the RegExp constructor to generate a regex from string:
const regex = caret => `[${caret ? '^' : ''}a-zA-Z0-9,?#&!#'"~ _;\\.\\*\\/\\-\\+ ]`
const regex1 = new RegExp(regex(true));
const regex2 = new RegExp(regex(false));
console.log(regex1.test('%'));
console.log(regex1.test('abc'));
console.log(regex2.test('abc'));
console.log(regex2.test('%'));

Pass variable values in the RegExp Javascript [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 7 years ago.
I want to pass variable values ​​in the rule expression . Someone any way ?
like is :
var a = 'khuya'; var regex = /{a}/;
Like this:
var a = "khuya";
var regex = new RegExp( "{" + a + "}" );

variable inside pattern [duplicate]

This question already has answers here:
How do I perform a Javascript match with a pattern that depends on a variable?
(3 answers)
Closed 8 years ago.
var seperator = ',', group = 'red, blue';
//group.search(seperator/g) - g is not defined
group.search(/seperator/g) // looks for "seperator"
group.search('/' + seperator + '/g') // doesn't seem to find my "seperator"
And with that I'm out of ideas... How do I get my seperator within expression?
Thanks in advance!
You need to create new regexp Object
var test = new RegExp(seperator, 'g');
group.search(test)

Categories