A selector string, jQuery object, DOM element, or array of elements indicating which element(s) to replace. g in the flags parameter.This version (at the moment of edit) does work in Firefox...... but, as noted at the very MDN page, its support will be dropped soon.it's IMPORTANT to use a regular expression because this:First of all, replace() is a javascript function, and not a jquery function.The above code replaces only the first occurrence of "." Well, there isn’t any JavaScript replace all method available out of the box but a JavaScript replace all regex is available. Hello highlight.js! First of all, replace() is a javascript function, and not a jquery function. But for most typical use cases, this is well worth not having to worry about special characters.For the sake of completeness, I got to thinking about which method I should use to do this. Follow me on Twitch, going to start streaming soon However, a dash.basically, this question is the same as the question here:@Mike, check the answer I gave there... regexp isn't the only way to replace multiple occurrences of a subsrting, far from it. so,As pointed out in a comment here, this will not work if your,There is another jsperf with variants on my recursive replace that go even faster (.If what you want to find is already in a string, and you don't have a regex escaper handy, you can use join/split:I like this method (it looks a little cleaner):The simplest way to this without using any regex is split and join like the code here:If the string contain similar pattern like.The previous answers are way too complicated. This doesn't work: text = text.replace('hey', 'hello'); Any idea?Sure Stefan, here is the code... text = text.replace(/hey/g, 'hello');Now this is one hell of an in depth answer! I was wondering what was happening... thanks !Firefox behaves like you have already passed /\./g as first parameter and replaces all instances.JavaScript .replace doesn't replace all occurrences [duplicate].How do I replace all occurrences of "/" in a string with "_" in JavaScript?Neural networks could help computers code themselves: Do we still need human…,Podcast 270: Oracle tries to Tok, Nvidia Arms up,Hot Meta Posts: Allow for removal by moderators, and thoughts about future…,Goodbye, Prettify. Replace all occurrences with regular expression The above code replaces only the first occurrence of "." This means it will be available for all string objects/literals.Say you want to replace all the 'abc' with 'x':I was trying to think about something more simple than modifying the string prototype.If you think of reuse, create a function to do that for you, but it's not recommended as it's only one line function, but again if you heavily use this, you can write something like this:and simply use it in your code over and over like below:But as I mention earlier, it won't make a huge difference in terms of lines to be written or performance, only caching the function may effect some faster performance on long strings and also a good practice of DRY code if you want to reuse.I also create my own solution. The value, or regular expression, that will be replaced by the new value,Required. Only the first occurrence will be r… the regular expression or if the first parameter is a string, include Perhaps (at risk of being voted down) that can be combined for a slightly more efficient but less readable form:This can be particularly useful when looking for duplicate strings.Although people have mentioned the use of regex but there's a better approach if you want to replace the text irrespective of the case of the text. It's not fullproof, of course, but it should be enough to get you started. It is treated as a literal string and is notinterpreted as a regular expression.