替换两个引号之间的字符串 [英] replace string between two quotes

查看:210
本文介绍了替换两个引号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个字符串 str =你好,我的名字是\michael \,你的是什么?进入你好,我的名字是< span class =name> michael< / span>

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>

我怎样才能在javascript中做到这一点?我知道怎么做str替换,但是如何获得我选择/替换它的内容。

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.

谢谢!!!

推荐答案

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

输出:

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

这篇关于替换两个引号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆