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);
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
my replace function only replace one character.
My Code example
const string = "What,Yo,Yes"
string.replace(","," ")
Console
"What Yo,Yes"
Something like this:
const string = "What,Yo,Yes";
console.log(string.replace(/,/g," "));
Alternatively, you can use replaceAll, like this:
const string = "What,Yo,Yes";
console.log(string.replaceAll(","," "));
This question already has answers here:
Replace all backslashes in a string with a pipe
(2 answers)
Closed 4 years ago.
I am trying to remove every backslash of a string for an hour, but I cannot make it work.
Here is my string for instance:
[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq
Here is what I tried:
const replaced = toString.replace(String.fromCharCode(92), String.fromCharCode(32));
const replaced = toString.replace("\\\\", "");
const replaced = toString.replace("\\", "");
const replaced = toString.replace(/\\/, "");
All of this does absolutely nothing.
You could use regex simply like :
var toString = '[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq';
console.log(toString.replace(/\\/g, ""));
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)
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
This javascript regex needs to extract "131PS" from "this is (131PS) for now".
Then I need to get "131" as a number and "PS" as a string.
Any suggestions? thx
myString.match(/\(([^\)]+)\)/ig)[0]
returns (131PS) which is not what was expected.
You need to work with capture regex groups () to withdraw the number and string separately, have a look:
let rawStr = "this is (131PS) for now";
let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/);
if (theMatch) {
let theNum = parseInt(theMatch[1]);
let theString = theMatch[2];
console.log(theNum, theString);
}
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