Arjay Bonus
Arjay Bonus

Reputation: 303

How can i insert a character in every specific character like "(" inside my string - javascript

for example:

x = "This is my string(string) another string(string)";

what i want to do is to insert a single quote after ( and before ) each.

Upvotes: 0

Views: 43

Answers (2)

3Dom
3Dom

Reputation: 815

Jack's answer is very short, which is good. If you're the kind of person who likes a process that easier to understand, but longer, here goes...

    var myString = "This is my string(string) another string(string)";

    myString = myString.split("(");
    myString = myString.join("('");
    myString = myString.split(")");
    myString = myString.join("')");

    console.log(myString);

Upvotes: 1

Jack
Jack

Reputation: 21183

Something like this:

var startStr = "This is my string(string) another string(string)";
var endStr = startStr.replace(/\(/g, '(\'').replace(/\)/g, '\'\)');
console.log(endStr); //This is my string('string') another string('string')

Upvotes: 2

Related Questions