Home » Javascript » how to change single parts of a string in javascript?

how to change single parts of a string in javascript?

Posted by: admin June 30, 2018 Leave a comment

Questions:

I want to accomplish the following:

var a = "hello"
var b 
b[0]=a[0] 

so that

  b[0] 
 >"h"

the first letter of b should be the same as the first letter of a so I want to assign a[0] (which is h) to b[0]. I don’t want to be restricted to the first letter, I want to use it to do things like

for(var i = 0; i<a; i++){
if( a.match(/[a-z]/i) !==null ){b[i] = " "}
else{b[i] = a[i]}

(replacing all the non-letters with spaces.) (this is just an example since I’ve been asked to clarify the question)

Answers: