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 \+
Related
This question already has answers here:
Insert a string at a specific index
(19 answers)
Closed 3 years ago.
For Example need to add a slash / in between USDYEN so it need to output USD/YEN.
One way is to FIND and replace USD for USD/ then it will output USD/YEN
However, how about if USD is not found because it's another currency let's say EUR/YEN?
The trick here is to slice or cut the string at a particular position for example at the third character then add a slash / then add the same string but without the first 3 characters.
var str = 'USDYEN'
// add a / in between currencies
// var newStr = str.slice(3) // removes the first 3 chars
// var newStr = str.slice(0,3) // removes the last 3 chars
var newStr = str.slice(0,3) + ' / ' + str.slice(3) // removes the first 3 and adds the last 3
console.log(newStr)
Here the string is being removed
More info into how to use slice()
https://www.w3schools.com/jsref/jsref_slice_string.asp
const pair = Array.from('USDGBP')
pair.splice(3, 0, '/')
console.log(pair.join(''))
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:
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.
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);
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);
}