Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

str.replace() bug? javascript

const str = 'test'
const newStr = str.replace(str[3],str[3].toUpperCase())
console.log(newStr) // output: 'Test'

const str2 = 'hello'
const newStr2 = str2.replace(str2[3],str2[3].toUpperCase())
console.log(newStr2) // output: 'heLlo'

what does go wrong?

expecting result: 'tesT'

expecting result: 'helLo'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.replace( x, y) Would replace the first occurance of x with y

The replace() method returns a new string with one, some, or all
matches of a pattern replaced by a replacement. The pattern can be a
string or a RegExp, and the replacement can be a string or a function
called for each match. If pattern is a string, only the first
occurrence will be replaced. The original string is left unchanged.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading