1

The current problem I'm having is that I'm received a string from the Database with ascii mixed with the string. I would like to know how to create a converstion function to change the string to the output below.

var string = 'Mc'Dought'; 

Output an end result of Mc'dought on the webpage.

function convert(str){
//Code Here
}
Blackspade
  • 45
  • 1
  • 5

2 Answers2

2

Use String#replace with a regular expression and a replacement function. You can parse the character code from each HTML entity using the regex, and then call String.fromCharCode to turn each code into its character form:

var string = 'Mc'Dought'

function convert (string) {
  return string.replace(/&#(?:x([\da-f]+)|(\d+));/ig, function (_, hex, dec) {
    return String.fromCharCode(dec || +('0x' + hex))
  })
}

console.log(convert(string))
gyre
  • 16,369
  • 3
  • 37
  • 47
  • Regular expressions are not particularly good for parsing if what you're parsing isn't regular. In this case, it might be but very often it isn't (e.g. parsing HTML). – RobG Mar 25 '17 at 01:09
  • Given OP's input string, this method looked appropriate enough, though I do understand that HTML parsing is far more nuanced when you start adding tags, inline styles and scripts, etc. Given that you've already marked this question as a duplicate, I will probably end up deleting this answer anyhow. – gyre Mar 25 '17 at 01:12
2

Be careful what you ask for.

You're looking at the escape sequence for single quote. If you throw it onto the page, it'll display properly.

On the other hand, if you insist on converting it to an actual single quote character, you could run into problems.

Check out this code snippet to see the consequences:

<INPUT Value="Mc&#039;Dought">
<BR>
<INPUT Value="Mc'Dought">
<BR>
<SPAN>Mc&#039;Dought</SPAN>
<BR>
<SPAN>Mc'Dought</SPAN>
<BR>
<INPUT Value='Mc'Dought'>  <!-- Problems!!  -->
<BR>
<SPAN>Mc'Dought</SPAN>

However, if you understand all this and still want to unescape it (e.g. you need to use the string to set the window title or issue an alert), I'd suggest you use an accepted HTML-unescape method, rather than replacing just &#039. See this answer.

Jquery:

function escapeHtml(unsafe) {
    return $('<div />').text(unsafe).html()
}

function unescapeHtml(safe) {
    return $('<div />').html(safe).text();
}

Plain Javascript:

function unescapeHtml(safe) {
    return safe.replace(/&amp;/g, '&')
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&quot;/g, '"')
        .replace(/&#039;/g, "'");
}
Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • The plain script version is much more complex than it needs to be. Parsing HTML with regular expressions is [*not recommended*](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – RobG Mar 25 '17 at 01:06