0

I'm bit new to JavaScript, I'm trying to replacing the array element using regex that matches the string, here is a code which I tried

<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>
<script>
function myFunction() {
    var abc = ["deno", "eno","pqr","lenovo"];
    var i,text;
    for(i = 0; i < abc.length; i++) {
        text += abc[i].replace(/no/i, "po");
        document.getElementById("demo").innerHTML = text;
    }
}
</script>

I want to replace array element with "po" wherever it encounters "no" in the array element string.

This is what I expect:

abc["depo","epo","pqr","lepovo"]
1
  • Expect array or string in output ? You have a typo as , in loop..jsfiddle.net/pkjsdu9w Commented Mar 31, 2016 at 7:19

3 Answers 3

1

You can do this for every element:

for(var i=0; i < abc.length; i++) {
    abc[i] = abc[i].replace('no', 'po');
}

or using one line

abc = abc.map(function(x){return x.replace('no', 'po');});

or using one line with "arrow functions":

abc = abc.map(x => x.replace('no', 'po'));

After you changed the array, you can convert it to a string using:

var text = 'abc['; 

for ( var i = 0 ; i < abc.length ; i++ ) {
    text+='\"'+abc[i]+'\"';
    if ( i != abc.length - 1) {
        text+=',';
    }
}
text += ']';

Test:

function myFunction() {
    var abc = ["deno", "eno","pqr","lenovo"];
    abc = abc.map(x => x.replace('no', 'po')); // see other 2 alternatives above

    var text = 'abc['; 

    for ( var i = 0 ; i < abc.length ; i++ ) {
        text+='\"'+abc[i]+'\"';
        if ( i != abc.length - 1) {
            text+=',';
        }
    }
    text += ']';
    document.getElementById("demo").innerHTML = text;
}
<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>

Sign up to request clarification or add additional context in comments.

2 Comments

After using your first solution ,its giving me this output denodepo,enoepo,pqrpqr,lenovolepovo which I think its taking the element first printing and then doing the replacement
@deepesh , I added the conversion to text, where I don't use replace anymore, because I've already changed the array. You can also see a snippet integrating all those parts. Click on Run code snippet to test it.
0
var i, text;
for(i = 0; i < abc.length, i++) {
  text += abc[i].replace("no", "po");
}
  console.log(text);

Comments

0

There are three changes required in your code:

  1. Initialiize text with empty string.Because it is undefined by default.
  2. Change abc[i].length to abc.length.
  3. Replace comma with a semicolon after abc[i].length in for loop.

    var abc = ["deno", "eno","pqr","lenovo"];
    var i;
    var text = "";
    for(i = 0; i < abc.length; i++) {
       text += abc[i].replace("no", "po");
    }
    

2 Comments

To your solution this is the answer I got depoepopqrlepovo
I tried using "document.getElementById("demo").innerHTML = text + "<br>"; " to break line but it didnt gave the desired result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.