1

I'm trying prepend all semicolons in a string with the HTML character  , which is a non-breaking thin space.

To do this, I have written this line:

str = str.replace( /;/g, " ;" );

Unfortunately, the thin space character is written in directly, instead of its HTML representation.

Is there a flag, a setting or even another function that will allow me to insert this character as HTML?

Thank you.

Edit: Probably should have mentioned this before: I'm working on a basic pseudocode-to-HTML converter, in which certain characters in the original string are replaced by their standard HTML equivalents.

The resulting string is then output to a div element, where I can inspect it to make sure it looks good. I then retrieve the source of this new text to place into some other web page.

But when I look at the source for the thin space, it's just the character itself, not the entity name.

(I'd paste the full source for the conversion, but it's over 300 lines.)

Edit 2: I was under the impression that Ctrl+U in Firefox would show the newly added characters. My mistake; they only appear when when viewing the source with Firebug (or its Webkit equivalent), or selecting the text, right-clicking and viewing the source for the selection (option only available in FF, it seems).

To test the latter approach, I created two div elements and used this JavaScript code:

document.getElementById( "div1" ).innerHTML = ( "'&'" );
document.getElementById( "div2" ).innerHTML = ( "' '" );

The ampersand is displayed as &, but the thin space's entity name is not used. This might just be weird behavior from this specific FF feature.

7
  • 3
    What really matters is what you do with the string after you've done that replacement. You should post that part of your code. Commented Jul 9, 2013 at 19:10
  • 1
    Why must the character be in entity form? Also, have you tried a replacement with " ;"? I think that may solve your problem, since & will be turned into an &. Commented Jul 9, 2013 at 19:10
  • I don't get it... you want a thin space characters, your you want the characters that make up the HTML entity for the thin space? Commented Jul 9, 2013 at 19:14
  • 1
    Entities are replaced by their corresponding character during HTML parsing, i.e. when you assign that string to .innerHTML of a DOM element. Commented Jul 9, 2013 at 19:16
  • @MathSquared11235: Thanks for the suggestion. I have tried it though, and it comes out as " ;" in the browser. What's strange is that the ampersand's entity name is preserved in the code, but the entity number I'm trying to use isn't. Commented Jul 9, 2013 at 19:46

3 Answers 3

1

" " is an html entity and therefore not understood by javascript. I recommend using the String.fromCharCode() method. An example would be as follows...

var str = 'test;here';

str = str.replace( /;/g, String.fromCharCode(8239) );

alert(str);

You can learn more about the fromCharCode method here.

Sign up to request clarification or add additional context in comments.

2 Comments

That's interesting. Since the parameter specifies a Unicode value, is the function encoding-independent? The main reason I want JS to insert the entity number is to ensure I don't have to worry about question marks if the encoding isn't right, but Mozilla's documentation doesn't address this.
The function will return a string using the default encoding, which gets a little tricky with javascript as there is no true right answer. The link below is the best I have found trying to explain javascript's internal encoding. I am no expert on this subject so hopefully this helps at least point you in the right direction. mathiasbynens.be/notes/javascript-encoding
1

If you literally want the characters  , the ampersand has to be encoded as such.

str = str.replace( /;/g, " ;" );

Comments

0

Why do you have the second semicolon in your replacement? Shouldn't it just be:

str = str.replace( /;/g, " " );

On my client 'str' contains the HTML entities but once you attach it to the DOM then the entities are changed into native characters. I guess it depends on what you're using it for. If you use 'str' as the innerText of an element then you'll see the entities and not the space. e.g.

(function(s) { var x=document.createElement('div'); x.innerText=s; document.body.appendChild(x); })(str);

2 Comments

I believe the second semicolon is because he still wants a semicolon in the end string. The first semicolon ends the HTML representation string; the second is a semicolon.
nevermind I re-read your post about why you have the double ;;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.