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
Related
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);
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:
Capture value out of query string with regex?
(9 answers)
Closed 3 years ago.
i have a cookie string like this
'user=sravan;XSRF-TOKEN=1212143;session=random'
i need to check for the XSRD-TOKEN in the cookie string, if we have the XSRF-TOKEN in the string then need to replace the value with 'test'
expected new string is 'user=sravan;XSRF-TOKEN=test;session=random'
i tried this (?<=XSRF-TOKEN).*$ but it is selecting the entire string after XSRF-TOKEN=
You could use (?<=XSRF-TOKEN=)([^;]+), example:
const str = 'user=sravan;XSRF-TOKEN=1212143;session=random';
const processed = str.replace(/(?<=XSRF-TOKEN=)([^;]+)/, "test");
console.log(processed);
But a better solution will be to parse the cookies and recreate the string.
This should only only select up until ;
(?<=XSRF-TOKEN)[^;]+
Or if you only like to select whats after = to ;
(?<=XSRF-TOKEN=)[^;]+
'user=sravan;XSRF-TOKEN=1212143;session=random'
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 8 years ago.
I want to replace all the words that match with a variable in a string, but it doesn't work. I use this code:
var findStr = "hello hi, test, hi";
var textSearch = "hi";
findStr = findStr.replace(textSearch,'<span>'+textSearch+'</span>');
It has to change the color of hi but only the first hi changes, the second one doesn't.
The replace method is what you're looking for.
For example: someString.replace("String you want to replace", "with this string")
This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');