Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <span class="name">michael</span>

How can I do that in javascript? I know how to do str replace, but how do I get the content that I selected/replaced with it.

Thanks!!!

like image 463
mazlix Avatar asked Dec 21 '25 06:12

mazlix


1 Answers

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 <span class="name">michael</span>, my friend's is <span class="name">bob</span>. what's your's?
like image 154
davin Avatar answered Dec 22 '25 18:12

davin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!