删除&害羞; 元素中的(软连字符)实体

Nat*_*röm 5 html css jquery html-entities

我正在尝试­从我的元素中删除所有实体(软连字符),

我一直试图用jquery来做这件事.当我从包含实体的html元素中获取文本时,我似乎得到一个字符串,其中实体被"隐藏"或无法编辑.

你是否必须做一些特别的事情来实际获得包含实体的字符串?

$( document ).ready(function(){
  $("button").on("click", function(){
    var html = $("div > span").html();
    var newHtml = html.replace("­", "");
    
    $("div > span").html(newHtml);
  });
});
Run Code Online (Sandbox Code Playgroud)
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span>My text will be hyphe&shy;ned</span>
</div>
<button>
  Remove Hyphens
</button>
Run Code Online (Sandbox Code Playgroud)

Pra*_*man 7

使用正则表达式:

var newHtml = html.replace(/\&shy;/gi, "");
Run Code Online (Sandbox Code Playgroud)

注意:您可能还需要检查&#173;,因为浏览器会以数字形式处理它,而不是人类友好的字符.

说明:

/(\&shy;)/gi
  1st Capturing group (\&shy;)
    \& matches the character & literally
    shy; matches the characters shy; literally (case insensitive)
    g modifier: global. All matches (don't return on first match)
    i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Run Code Online (Sandbox Code Playgroud)

片段

$( document ).ready(function(){
  $("button").on("click", function(){
    var html = $("div > span").html();
    var newHtml = html.replace(/(\&shy;|­|&#173;)/gi, "");
    
    $("div > span").html(newHtml);
  });
});
Run Code Online (Sandbox Code Playgroud)
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span>My text will be hyphe&shy;ned</span>
</div>
<button>
  Remove Hyphens
</button>
Run Code Online (Sandbox Code Playgroud)

Regex101:https://regex101.com/r/wD3oX7/1