簡體   English   中英

替換兩個引號之間的字符串

[英]replace string between two quotes

我想轉一個字符串str = "hello, my name is \"michael\", what's your's?" 進入"hello, my name is <span class="name">michael</span>

我怎么能在 javascript 中做到這一點? 我知道如何進行 str 替換,但是如何獲取我選擇/替換的內容。

謝謝!!!

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?

雖然@davin 的回答是正確的,但我假設您需要處理雙引號的實例。

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

一個*代替+就足夠了:

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

盡管您可以通過簡單的首次解析搜索和替換完全放棄它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM