javascript – How to take a substring from a string and attach another string to the string and convert it to an array

javascript – How to take a substring from a string and attach another string to the string and convert it to an array

Use a regular expression to extract the values between the square brackets, and then map and join them:

const inputs = [
Exit Survey [hr.exitinterview], Certificate [hr.certificate], Recommendation Letter [hr.recommendation],
Exit Survey [aaa], Certificate [bbb], Recommendation Letter [ccc],
Exit Survey [qqqqq], Certificate [qwert], Recommendation Letter [12345],
];
const outputs = inputs.map((input) => {
  const strings = input.match(/[^[]]+(?=])/g);
  const newFullStr = strings
    .map(str => https://somelink/ + str)
    .join(, );
  return newFullStr;
});
console.log(outputs);

Array methods are far superior to for loops in most cases.

javascript – How to take a substring from a string and attach another string to the string and convert it to an array

Leave a Reply

Your email address will not be published. Required fields are marked *