Suliman Sharif Suliman Sharif - 3 months ago 9
jQuery Question

How do I replace a specific part of a string in Javascript

I have a string that looks like this

ATOM LEU 0.1234 C 0.123 0.32 0.34

How do I replace the C with .replace() to & in Javascript by getting only the location of C? The letter of C can essentially be anything.

I want

ATOM LEU 0.1234 & 0.123 0.32 0.34.

Answer

You can do it by using substring(),

var str = "ATOM LEU 0.1234 C 0.123 0.32 0.34";
var res = str.substring(0, 16) + "&" + str.substring(17);
console.log(str); //"ATOM LEU 0.1234 & 0.123 0.32 0.34"