SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression: need to display "&" on screen

    Hi Everyone,

    I have a problem with the "&" character and one character following the "&" displaying.

    For example:
    P&E test ~~~~~~~~~~~~ this displays with the whole initial string: P&E test

    P&E ~~~~~~~~~~~~~~~ only the P displays not the original string: P&E

    P & E ~~~~~~~~~~~~~~ displays the whole initial string:P & E


    This is the code I'm using to try and solve that problem.
    The first alert gives me the message I want from a database read.
    the second alert takes off the &E. So it's coming in, just not displaying out.

    var note = note.innerHTML;
    alert(note);
    note = note.replace(/\&/g, "\&");
    alert(note);

    thank you

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To display an & on the page you need to specify it as &
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    thanks for getting back to me. The &s display just fine when they're nested just not when more text or single letter with no spaces immediately follow the &.


    So the & displays here: P & E test but not with P&E. I only get the P to show up then.
    This is all happening in a dynamically generated table so I can't specify it in each instance at the end of the line of text. That's why I thought using the regex might be the way to go.

    thanks

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    note = note.replace(/&/g, "&amp;");

    There are four characters that have special meanings in HTML

    < start of tag
    > end of tag
    & start of entity
    ; end of entity

    To display any of the first three of these in the actual page you need to use the equivalent entity code &lt; &gt; &amp; respectively.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    May 2005
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello
    thanks for the suggestion. Here is what I ended up with:
    notes = notes.replace(/\&/g, "\&amp;");
    newCell.innerHTML = notes;

    thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •