<!DOCTYPE html>
<html lang="en" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RTL Table with LTR English Text</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
td {
border: 1px solid #ddd;
padding: 8px;
}
/* Optional styling for the LTR text */
.ltr-text {
direction: ltr;
display: inline-block;
}
</style>
</head>
<body>
<table>
<tr>
<td>هذا نص باللغة العربية وبعض English words.</td>
<td>Here is some more text بالعربية and English phrases like this.</td>
</tr>
<tr>
<td>RTL نص هنا مع phrase of English words.</td>
<td>Another example النص مع a full sentence in English في النهاية.</td>
</tr>
</table>
<script>
// Function to detect English phrases in a string
function wrapEnglishTextInLTR(text) {
// The updated regex looks for sequences of English words (separated by spaces or punctuation)
return text.replace(/([a-zA-Z\s]+(?:[.,!?])?)/g, '<span class="ltr-text" dir="ltr">$1</span>');
}
// Apply the transformation to all table cells
const tableCells = document.querySelectorAll('td');
tableCells.forEach(cell => {
cell.innerHTML = wrapEnglishTextInLTR(cell.innerHTML);
});
</script>
</body>
</html>
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.