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)
Related
This question already has answers here:
Regex to allow alphanumeric and dot
(3 answers)
Closed 2 years ago.
I have this code:
var text = "test+subject co+vid banana";
var words = (text.match(/\w+/mg));
var random = [];
var rn = Math.floor(Math.random() * words.length);
random.push( words[rn]);
console.log(words.splice(rn, 1));
The variable random gets set to "test" or "co" instead of "test+subject" or "co+vid". What have I done wrong?
EDIT: Sorry, I was unclear; I also want it to catch single words without a plus (like the banana I added to the list)
The + is a special regex-character. If you want to match it literally you have to escape it using \+
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);
This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Closed 5 years ago.
If I have this string: "MyStringToForSplitting" and I want to split it by this word "ring".
If I do this: "MyStringToForSplitting".split('ring')
I will get array that contains this elements:
MyS
ToForSplitting
I can't figure out best way to split it and include 'ring' in resulting array like this:
1.MyS
2.ring
3.ForSplitting
You can use regex and capture the split pattern:
console.log("MyStringToForSplitting".split(/(ring)/));
var s = "ring";
var p = new RegExp("(" + s + ")")
console.log("MyStringForSplitting".split(p))
This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
I have the following url
http://www.test.info/?id=50&size=40
How do I get the value of the url parameter with regular expressions in javascript . i need the size value and also need the url without &?
only
http://www.test.info/?id=50
Thanks
Consider using split instead of a regex:
var splitted = 'http://www.test.info/?id=50&size=40'.split('&');
var urlWithoutAmpersand = splitted[0];
// now urlWithoutAmpersand => 'http://www.test.info/?id=50'
var sizeValue = splitted[1].split('=')[1] * 1;
// now sizeValue => 40
Just use this as your regex
size.*?(?=&|$)
here is some code you can use
var re = /size.*?(?=&|$)/g;
var myArray = url.match(re);
console.log(myArray);
you also can do it like this:
var re = new RegExp("size.*?(?=&|$)", "g");
Here is a regex pattern you could use.
^(.+)&size=(\d+)
The first group will be the url up to right before the '&' sign. The second group will be the value of the size parameter. This assumes id always comes before size, and that there are only two parameters: id and size.
This question already has answers here:
How to replace multiple keywords by corresponding keywords with the `replace` method?
(3 answers)
Closed 7 years ago.
This is a chunk of Google Apps Script code:
var re = /(<.*?>)+/;
var strip = str.replace(re, "");
Logger.log(strip);
Why does it strip only the first instance of tag?
var re = /(<.*?>)/g
The trailing g is a flag you need to set to replace all matching instances. Depending on the content of str you are passing Another flag you may wish to try adding is m which signifies that the pattern should apply to multiple lines i.e.
var re = /(<.*?>)/mg