如何用jQuery移除font-family中的"并保留其他引用符号?
Got it, let's tackle this problem step by step. You need to specifically remove the " entities that wrap font names in the font-family CSS property, while leaving identical entities in regular text (like vishal"s) untouched. Here's how to pull this off with jQuery:
Step-by-Step Implementation
Here's a straightforward jQuery snippet tailored to your needs:
// Get the raw HTML content from your text box (update the selector to match your element's ID/class) var originalContent = $('#your-textbox-selector').val(); // Use a targeted regex to replace ONLY the " inside font-family declarations var modifiedContent = originalContent.replace(/font-family:"([^&]+)"/g, 'font-family:$1'); // Push the cleaned content back into the text box $('#your-textbox-selector').val(modifiedContent);
How the Regex Works
Let’s break down the regex to make sure you understand why it doesn’t affect your desired text:
font-family:": Explicitly targets the start of a font-family property with the opening escaped quote.([^&]+): Captures the actual font name (e.g.,arial black) — this matches any characters except&, so it stops right before the closing".": Matches the closing escaped quote for the font name.- The
gflag ensures we replace every occurrence of this pattern in the content, not just the first one.
Example Test
If your original content is:
Hi this is vishal&quot;s code <span style='font-family:&quot;arial black&quot;'>vishal</span>
After running the code, the processed content becomes:
Hi this is vishal&quot;s code <span style='font-family:arial black'>vishal</span>
Exactly what you need — the &quot; in vishal&quot;s stays intact, while the ones wrapping arial black are removed cleanly.
Edge Case Handling
If your RadEditor sometimes uses double quotes for the style attribute (instead of single quotes) or adds whitespace after font-family:, adjust the regex to be more flexible:
var modifiedContent = originalContent.replace(/font-family:\s*&quot;([^&]+)&quot;/g, 'font-family:$1');
The \s* allows for optional spaces/tabs after font-family:, so the regex works even if the editor’s HTML formatting varies slightly.
内容的提问来源于stack exchange,提问作者user3844820

