1
var result = "I am a dog";
var searchterm = "a";

How do I insert "(" before every "a" and ")" after every "a" so the new result is

newresult = "I (a)m (a) dog"

2 Answers 2

5

You can use replaceAll method.

var result = "I am a dog";
var searchterm = "a";

const output = result.replaceAll(searchterm, `(${searchterm})`);
console.log(output);

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

1 Comment

apprve the answer would be appreciated if it's helpful @Ariel
0

let result = "I am a samovar";

const searchTerms = [" am ", " a "];
const replacements = [" (a)m ", " (a) "];

searchTerms.map((term, i) => { 
    result = result.replaceAll(term, replacements[i]) 
})

console.log(result)

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.