0

If I have an array element of the following format (with one whitespace character between each pair of letters, ignore the quote marks which are not part of the actual element):

arr[1] = "AB CD EF GH"

How can I edit this to be of the following format (with each pair of letters surrounded by * and two spaces between each pair of letters):

arr[1] = "*AB*  *CD*  *EF*  *GH*"

2 Answers 2

2

Use split, map, then join:

arr[1] = arr[1].split(' ').map(function (el) {
  return '*' + el + '*';
}).join('  ');

DEMO

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

Comments

1

Replace the single space:

arr[1] = "*" + arr[1].replace(/ /g, "*  *") + "*";

alert(arr[1]);

Comments

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.