Skip to content Skip to sidebar Skip to footer

Replace String Between Two Quotes

I want to turn a string str = 'hello, my name is \'michael\', what's your's?' into 'hello, my name is michael How can I do that in javascript?

Solution 1:

str ="hello, my name is \"michael\", my friend's is \"bob\". what's yours?";
str.replace(/"([^"]+)"/g, '<span class="name">$1</span>');

Outputs:

hello, my name is <spanclass="name">michael</span>, my friend's is <spanclass="name">bob</span>. what's your's?

Solution 2:

While @davin's answer is correct, I assume you'll want to deal instances of double quotes.

var str ="hello, my name is \"michael\", what's your's? Is is \"James\" or is it blank:\"\"";

A * in place of the + will suffice:

/"([^"]*)"/g

Although you could discard it altogether with a simple first-parse search and replace:

str.replace(/""/,"")

Post a Comment for "Replace String Between Two Quotes"