【问题标题】:Left to right mark in regex (‎) JavaScript正则表达式中的从左到右标记 (‎) JavaScript
【发布时间】:2026-02-20 16:05:01
【问题描述】:

我有这个 html(希伯来语文本):

בדיקה בדיקה בדיקה‎ניסיון ניסיון ניסיון

我想通过将 (‎) 替换为空格来删除它。

我尝试使用正则表达式:

$('#result').html($('#test').html().replace(/‎/, ' '));
$('#result2').html($('#test2').html().replace(/©/, ' '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="test">   
בדיקה בדיקה בדיקה&lrm;ניסיון ניסיון ניסיון
  </div>
<hr />
<div id="test2">   
בדיקה בדיקה בדיקה&copy;ניסיון ניסיון ניסיון
  </div>
<hr />
<div id="result"></div>
<hr />
<div id="result2"></div>

我添加了复制示例以表明我可以替换复制字符,因为它是可见符号。但是如何替换从左到右的符号呢?

【问题讨论】:

  • 如何提问的好例子(这也是一个有趣的问题)
  • 试试$('#result').html($('#test').html().replace(/\u200E/, ' '));

标签: javascript html regex special-characters


【解决方案1】:

当您检索 HTML 时,显然 LTR 标记并没有被转换为字符实体,它只是 Unicode 字符 200E

因此,为了允许将其设为实体的浏览器和不设为实体的浏览器,请在 &amp;ltr;\u200E 之间使用交替:

var html = $('#test').html();
var rep = html.replace(/&lrm;|\u200E/gi, ' ');
$('#result').html(rep);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="test">   
בדיקה בדיקה בדיקה&lrm;ניסיון ניסיון ניסיון
</div>
<hr />
<div id="result"></div>

我还允许实体使用大写 (&amp;LTR;) 或小写 (&amp;ltr;),并添加了 g 标志以在整个字符串中替换它(删除 @ 987654330@如果你只想替换第一个)。

【讨论】: