string.replace against html symbol code (÷)? |
string.replace against html symbol code (÷)? |
CharliePrince |
Jan 9 2021, 03:38 AM
Post
#1
|
Novice Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 |
I'm trying to replace an html symbol code '÷' with another string.
I can't get this to work. See example below. CODE <body> <p id='target'>A ÷ B</p> <script> console.log(`${document.getElementById('target').innerText}`); console.log(`${document.getElementById('target').innerText.replaceAll('÷', '/')} (want this to = 'A / B') `); console.log(`${document.getElementById('target').innerText.replaceAll('÷', '/')} (or, want this to = 'A / B') `); </script> </body> Can anyone help? Pandy? |
CharliePrince |
Jan 9 2021, 04:26 PM
Post
#2
|
Novice Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 |
I didn't notice your most recent posts here until after my last reply above.
Yes - it's odd to me why some of these are interpreted or rendered literally or as their encoded char or whatever. Crap! I don't know the words to describe but I think you and Christian know what I mean Sorry about the console.logs but . . Look at this (below) . . . if it's of any interest where currentString is broken down to each character CODE console.log(currentString); for (i = 0; i < currentString.length; i++) { console.log(currentString[i] + ' = ' + currentString.charCodeAt(i)); } |
Christian J |
Jan 9 2021, 08:47 PM
Post
#3
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
I don't know the words to describe but I think you and Christian know what I mean We sure do. I usually reserve this smilie for situations like this: QUOTE Sorry about the console.logs but . . Look at this (below) . . . if it's of any interest where currentString is broken down to each character CODE console.log(currentString); for (i = 0; i < currentString.length; i++) { console.log(currentString[i] + ' = ' + currentString.charCodeAt(i)); } I don't understand, will it not just return the charCode for each character in currentString? Except if you feed it CODE & of course, then it will return CODE & & = 38 a = 97 m = 109 p = 112 ; = 59 |
pandy |
Jan 10 2021, 04:25 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
HTML <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('\367', '/'); document.getElementById('target').innerHTML = replacestring; } If the entity is one of the not 'interpreted' ones it doesn't work right off, because there javascript seems to see the HTML entity as a string of characters. For ampersand I had to do this or only the literal ampersand would be replaced, not the whole entity. I find this very confusing, because if it is a sting, why can't it be replaced the normal way? HTML <p id='target'>A & B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var entity = '\46' + 'amp;'; var replacestring = currentstring.replace(entity, 'and'); document.getElementById('target').innerHTML = replacestring; } So it gets hairy anyway. Not for a unique case like with divide, but if you don't know what entities there may be. There could be more 'not interpreted' ones than ones we've found. There must be some documentation for this! Maybe one could check for & and ; and use different methods accordingly, but those characters could occur otherwise in the text too, so I don't really see how this could work unless a list of not interpreted entities is used... |
Christian J |
Jan 10 2021, 03:18 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
HTML <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('\367', '/'); document.getElementById('target').innerHTML = replacestring; } Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> --why isn't the entity "interpreted" now? Is the replace() method different? |
pandy |
Jan 10 2021, 03:30 PM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> We already knew that. CODE --why isn't the entity "interpreted" now? Is the replace() method different?:unsure: And that too. It is interpreted, but it doesn't work to use the actual character for the replace. Those too things are what was so darn odd! I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. https://brajeshwar.github.io/entities/ |
Christian J |
Jan 10 2021, 04:16 PM
Post
#7
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. https://brajeshwar.github.io/entities/ Seems I already had that page bookmarked, we must have been discussing this before. |
pandy |
Jan 10 2021, 04:46 PM
Post
#8
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
Nope. I didn't know about this and I hadn't seen that page. Well, I knew that kind of octal codes could be used in JS, but I didn't know they needed to be used in replace strings.
I may add that I still don't understand why they need to be used... In a case like this the ampersand doesn't need to be escaped. What's the difference? CODE var test = 'this & that'; alert(test); Are there other cases when these characters need escaping? |
Christian J |
Jan 10 2021, 07:56 PM
Post
#9
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
CSS generated content? Maybe I saw that page when reading this: https://mathiasbynens.be/notes/css-escapes
|
pandy |
Jan 10 2021, 08:10 PM
Post
#10
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
Haven't seen that page either. The only things I've read about CSS escape characters is in the spec and maybe some book, I think.
|
Lo-Fi Version | Time is now: 28th April 2024 - 11:29 AM |