This question already has answers here:
Regular Expression to select everything before and up to a particular text
(6 answers)
Closed 3 years ago.
I want to ignore the values after -w2 and extract 'JLC-22 VILA'
var item="JLC-522 VUOTILA-w2",
item.replace('-w','')
I want to ignore the values after -w2 and extract 'JLC-22 VILA'
The item value is dynamic item values keeps changing like "JLC-22 VILA-w18"
"JBC-12 KULA-w23"
Match any characters, while looking ahead for -w after the end of the match:
var item="JLC-522 VUOTILA-w2";
const output = item.match(/.+(?=-w)/)[0];
console.log(output);
If you are sure that you will not have -w you wanna keep you can use split function
const item="JLC-522 VUOTILA-w2";
const str = item.split('-w')[0];
console.log(str);
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
I want to remove all "0" in my string, not only the first same value, any suggest?
Why its work but just only the first code
var str = "90807005"
console.log(str.replace("0",""))
I try to read another source and say to use (/"something"/g, new) for change all same value, and its still not working
var str = "90807005"
console.log(str.replace(/"0"/g,""))
I want it to be str = "9875";
You can use String.replaceAll or a global flag in your regex:
var str = "90807005"
console.log(str.replaceAll("0","")) //replaceAll
console.log(str.replace(/0/g,"")) //global flag
This question already has answers here:
Cutting a string at nth occurrence of a character
(5 answers)
Closed 1 year ago.
I am having a string like: $m-set 88829828277 very good he is. From this string I want to get the part after the second space. ie, I want to get: very good he is.
I tried using split(" ")[2] but it only gives one word: very.
Any help is greatly appreciated!
Thanks!
While you could split and join:
const input = '$m-set 88829828277 very good he is';
const splits = input.split(' ');
const output = splits.slice(2).join(' ');
console.log(output);
You could also use a regular expression:
const input = '$m-set 88829828277 very good he is';
const output = input.match(/\S+ \S+ (.+)/)[1];
console.log(output);
where the (.+) puts everything after the second space in a capture group, and the [1] accesses the capture group from the match.
This question already has answers here:
Test If String Contains All Characters That Make Up Another String
(4 answers)
Closed 2 years ago.
I have been struggling with a regex pattern to find out if a string contains all characters in a set of characters.
e.g I want to test if "sheena" contains esnh, I should get a match but if I want to test if "sheena" contains esnk, I should not get a match because there exist no k in "sheena" even thought esn exists.
To solve this problem just use the every method.
const matchString = (str, matcher) => [...matcher].every(n => str.includes(n));
console.log(matchString('sheena', 'eshn'));
console.log(matchString('sheena', 'eshk'));
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
So I'm trying to make an a sort of auto correct system in my website, and I want to detect a word and if the word is in the image source, then replace it. In my code I am trying to replace the word "Dunk" with SB Dunk. In the source it says Dunk twice, but it only replaces the first time it uses "Dunk", and then keeps making errors and keeps adding more "SB's" to the "Dunk". Heres my code.
//https://stockx-360.imgix.net//Nike-Dunk-Low/Images/Nike-Dunk-Low/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806`.replace('%20', '-')
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/, 'SB Dunk');
shoesimg.src = newshoesimg;
}
You can use replaceAll. Following is the example,
let replacedStr = "1 abc 2 abc 3".replaceAll("abc", "xyz");
// replacedStr is "1 xyz 2 xyz 3"
Another way would be to find the indexes of the word dunk and then using for loop replace one-by-one.
Just add 'g' at the end of regular expression:
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/g, 'SB Dunk');
shoesimg.src = newshoesimg;
}
https://dmitripavlutin.com/replace-all-string-occurrences-javascript/
Try String.replaceAll():
const newshoesimg = shoesimg.src.replaceAll('Dunk', 'SB Dunk');
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);