.replace all variables in string [duplicate] - javascript

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

Related

Removing all spesific value in string of number in Node.js [duplicate]

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

Is there is a way to replace multiple character with replace function? [duplicate]

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(","," "));

javascript replace instance of string [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 5 years ago.
I want to replace all instance of particular string using javascript/jquery.
My source string is '\\n' and my desired output is '\n'.
this should happen at all instance with in a string.
var str = "hii All, \\n I am sambo. \\n Thank you for listening.";
var res = str.replace(/\\n/gi, "\n");
console.log(str);
Use replace with regex.

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

Categories