variable inside pattern [duplicate] - javascript

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)

Related

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);

regex lazy repeat [duplicate]

This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
How to match multiple occurrences of a substring
(3 answers)
Closed 4 years ago.
I am trying to replace 「.file extension,」 into 「,」
「1805171004310.jpg,1805171004311.png,1805171004312.jpg,」 into 「1805171004310,1805171004311,1805171004312,」
How can I make it lazy and repeat?
https://jsfiddle.net/jj9tvmku/
dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';
fileNameWithoutExt = dataArr[1].replace('/(\.(.*?),)/', ',');
$('#msg').val(fileNameWithoutExt);
https://regex101.com/r/nftHNy/3
Just use the global flag g.
Your regex, isn't actually a regex, it's a string. Remove the single quotes surrounding it: /(\.(.*?),)/g, And you can remove all the capture groups, since are not needed here: /\..*?,/g
const dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';
const fileNameWithoutExt = dataArr[1].replace(/\..*?,/g, ',');
console.log(fileNameWithoutExt);
// or an array of filenames
console.log(fileNameWithoutExt.split(',').filter(Boolean));
If you want the file names individually, use .split(',').filter(Boolean)

Return everything after phrase, to end of string, excluding phrase [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
I have the following URL
https://website.com?id=XXXVVVCCCHHH
I only want XXXVVVCCCHHH, I've tried the following:
var phrase = 'https://website.com?id=XXXVVVCCCHHH';
var myRegexp = /id=(.*)/;
phrase = myRegexp.exec(phrase);
But this is returning: id=XXXVVVCCCHHH;
How can Ii edit this to only return XXXVVVCCCHHH?
Just use split and take the second element:
var url = "https://website.com?id=XXXVVVCCCHHH";
var part = url.split('=')[1];
console.log(part);

Regex with variables in it [duplicate]

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

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 + "}" );

Categories