Regular expression removing only first instance [duplicate] - javascript

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

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

javascript Regular Expressions [duplicate]

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.

Javascript replace() only replaces first match [duplicate]

This question already exists:
Javascript multiple replace [duplicate]
Closed 9 years ago.
Hello see the jsfiddle here : http://jsfiddle.net/moolood/jU9QY/
var toto = 'bien_address_1=&bien_cp_1=&bien_ville_1=';
var tata = toto.replace('&','<br/>');
$('#test').append(tata);
Why Jquery in my exemple only found one '&' and replace it?
Because that's how replace works in JavaScript. If the search argument is a string, only the first match is replaced.
To do a global replace, you have to use a regular expression with the "global" (g) flag:
var tata = toto.replace(/&/g,'<br/>');
The code that you have written will only replace the first instance of the string.
Use Regex along with g will replace all the instances of the string.
toto.replace(/&/g,'<br/>');

Categories