Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ­ (soft hyphen) entity from element

I'm trying to remove all ­ entities (soft hyphens) from my element,

I've been trying to do this using jquery. When I fetch the text from the html element containing the entity I seem to get a string where the entity is "hidden" or can't be edited.

Do you have to do something in particular to actually get a string including the entities?

$( document ).ready(function(){
  $("button").on("click", function(){
    var html = $("div > span").html();
    var newHtml = html.replace("­", "");
    
    $("div > span").html(newHtml);
  });
});
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
<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>
like image 785
Natalie Hedström Avatar asked Dec 02 '15 12:12

Natalie Hedström


People also ask

How do you remove a background?

Here's our top five best apps to remove background from photo for iPhone and Android in 2022: YouCam Perfect: Best Free Background Remover. PhotoCut Remove Background PNG. Magic Eraser Background Editor.

Is remove BG free?

They are FREE on remove.bg and significantly cheaper than regular images (¼ credit instead of 1 credit) when requested through the API.


1 Answers

Use regex:

var newHtml = html.replace(/\&shy;/gi, "");

Note: You may also need to check for &#173;, because browser treats it in numbers and not in human friendly chars too.

Explanation:

/(\&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])

Snippet

$( document ).ready(function(){
  $("button").on("click", function(){
    var html = $("div > span").html();
    var newHtml = html.replace(/(\&shy;|­|&#173;)/gi, "");
    
    $("div > span").html(newHtml);
  });
});
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
<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>

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

like image 163
Praveen Kumar Purushothaman Avatar answered Oct 13 '22 10:10

Praveen Kumar Purushothaman