我想将字符串str = "hello, my name is \"michael\", what's your's?"
转换为"hello, my name is <span class="name">michael</span>
我怎样才能在javascript中执行此操作?我知道如何进行str替换,但是如何获取我选择/替换它的内容。
感谢!!!
答案 0 :(得分:7)
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?
答案 1 :(得分:0)
虽然@davin的回答是正确的,但我认为你会想要处理双引号的实例。
var str = "hello, my name is \"michael\", what's your's? Is is \"James\" or is it blank:\"\"";
*
代替+
就足够了:
/"([^"]*)"/g
虽然您可以通过简单的首次解析搜索完全丢弃它并替换:
str.replace(/""/,"")