ASP.NET MVC中Oracle数据库chr(13)格式邮件模板的显示、编辑与保存问题求助
Let's break this down into two clear, actionable parts: converting the stored template to a user-friendly editable format, and then converting the edited content back to Oracle's required format.
1. Convert Oracle's CR-based Template to Visible Line Breaks
First, let's clarify: your Oracle database stores actual carriage return characters (chr(13) translates to ASCII 13, or \r in most programming languages), not the string chr(13) itself. The issue with your regex attempt was likely targeting the wrong pattern—you need to match the actual \r characters, not the text "chr(13)".
Option 1: Backend Processing (Python/Java Examples)
If you're handling this on the backend before sending to the frontend:
Python
import re # raw_content = value fetched directly from Oracle database formatted_for_editor = re.sub(r'\r+', '\n', raw_content).strip() # Optional: Remove extra blank lines to match your desired clean format formatted_for_editor = re.sub(r'\n\s*\n', '\n', formatted_for_editor)
Java
String rawContent = // value retrieved from Oracle; String formattedForEditor = rawContent.replaceAll("\\r+", "\n").trim(); // Optional: Clean up redundant blank lines formattedForEditor = formattedForEditor.replaceAll("\\n\\s*\\n", "\n");
Option 2: Frontend Processing (JavaScript)
If you want to handle formatting directly in the browser for a textarea input:
// rawText = text fetched from your backend API const displayText = rawText .replace(/\r+/g, '\n') // Replace consecutive CRs with single line feeds .replace(/\n\s*\n/g, '\n') // Remove extra empty lines .trim(); // Assign to your textarea element document.getElementById('email-template-editor').value = displayText;
This will produce the clean, line-broken format you outlined in your example.
2. Convert Edited Content Back to Oracle's Format
Once the user finishes editing the template, you need to convert the line feed-based breaks (\n) back to carriage return characters (\r, which maps to chr(13) in Oracle) for storage.
Backend Conversion (Python/Java Examples)
Python
# edited_content = text submitted by the user (with \n line breaks) oracle_ready_content = edited_content.replace('\n', '\r') # Always use parameterized queries to insert this value into Oracle (prevents SQL injection)
Java
String editedContent = // user-submitted text from your form; String oracleReadyContent = editedContent.replace("\n", "\r"); // Insert using prepared statements to ensure safety and compatibility
Edge Case: If Your Database Stores SQL Snippets (Not Raw Text)
If for some reason your database stores the literal SQL concatenation string (e.g., 'Hello,' || chr(13) || '...'), here's how to handle that:
Convert to Editable Format
const rawSqlString = `'Hello,' || chr(13) || ' ' || chr(13) || '' || chr(13) || 'You are now a registered user'`; const displayText = rawSqlString .replace(/'\s*\|\|\s*chr\(13\)\s*\|\|\s*'/g, '\n') .replace(/^\'|\'$/g, '') // Remove leading/trailing single quotes .replace(/''/g, '') // Clean up empty string placeholders .trim();
Convert Back to SQL Snippet
const editedText = document.getElementById('email-template-editor').value; const oracleSqlString = `'${editedText.replace(/\n/g, "' || chr(13) || '")}'`;
Note: Storing SQL snippets like this is not ideal—storing raw text with actual line breaks is more maintainable and secure.
内容的提问来源于stack exchange,提问作者Boyan Mihnev

