Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2006, 08:16 PM   PM User | #1
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
Character Replacing in HTML (Cross-Browser Code) How?

Hi ..

I have an adea to replace some charactor by another one in web pages by JavaScript.

Example(1) - One Charactor Replacing :
----------------------------------------------------------------
I need to replace:
any "#" charactor to "@"
any "a" to "A",
any "\n" to " ",
and any "~" to "&".

Example(2) - Word or Phrase Replacing :
----------------------------------------------------------------
Replacing a list or array of bad words to other words.


I don't know how I can do that for all document items like: <body>, <td>, <th>, <div>, <span>, <p> and so on.
but I need to check a full HTML page not only those tags.
Also I need a cross-browser code using DOM.

I think we can use something like this:
Code:
  
item.innerHTML

or:

document.layers.innerHTML // for Geko engine browsers.
document.all.innerHTML // for IE browser.
I can do this simply in PHP language functions like str_replace() but it's a server-side language, I want a cleint-side method by JavaScript.
Can any JavaScript professional help me ?
alMubarmij is offline   Reply With Quote
Old 12-30-2006, 07:47 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Example 1.

The general form of the regex is
word = word.replace (/\a/g, "A"); // replace all instances of a by A
word = word.replace (/\~/g, "@");
// replace all instances of ~ by @


Example 2.

See:
http://www.js-examples.com/page/java...le.html?id=964

http://www.irt.org/script/229.htm
Philip M is offline   Reply With Quote
Old 12-31-2006, 12:40 PM   PM User | #3
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
Thanks I checked links, but nothing useful for me yet.
Can you explain more about your code and tel me how to use it for Example 1 ?

Last edited by alMubarmij; 12-31-2006 at 03:54 PM..
alMubarmij is offline   Reply With Quote
Old 12-31-2006, 01:38 PM   PM User | #4
vegu
New Coder

 
Join Date: Dec 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
vegu is on a distinguished road
loop through all elements by calling a function recursively like

Code:
function scanNodes(node) {
  var i;
  for(i = 0; i < node.childNodes.length; i++)
    scanNodes(node.childNodes[i]);
}
Then while looking through all those nodes, check if theyre text nodes by checking their nodeName attribute, and if it is remove this text node and re-add with the replaced characters.

Code:
function scanNodes(node) {
 
  var i;

  if(node.nodeName == '#text') {
    
    /* get the old text held by the text node */

    var oldText = node.nodeValue;
    
    /* create a new text node with the characters replaced, in the case below  
     * the character a is replaced by A, look up regex with google if you need 
     * know more
     */
   
    var newNode = document.createTextNode(oldText.replace(/a/g, 'A'));
    
    /* insert the new node before the old node, to assure it will be in the correct spot */    

    node.parentNode.insertBefore(newNode, node);
    
    /* remove the old node */
     
    node.parentNode.removeChild(node);
    
    /* bail since text nodes dont have children */

    return;
  }

  /* recursively loop through all child nodes */  

  for(i = 0; i < node.childNodes.length; i++)
    scanNodes(node.childNodes[i]);
}
In order to make my example work youd call scanNodes on the body element

Code:
<body onload="scanNodes(document.body)">
__________________
http://demo.vegui.org - vegUI AJAX framework
http://www.landsofkazram.com - browser based graphical MMORPG
vegu is offline   Reply With Quote
Old 01-01-2007, 06:13 PM   PM User | #5
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
Sorry I am not advanced in JavaScript.

Can you put a full page form my example1:
Quote:
Example(1) - One Charactor Replacing :
--------------------------------------
I need to replace:
any "#" charactor to "@"
any "a" to "A",
any "\n" to "&nbsp;",
and any "~" to "&".
alMubarmij is offline   Reply With Quote
Old 01-01-2007, 07:28 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
What is the point of this? What are you trying to achieve? You cannot make changes to other peoples' web pages.
Philip M is offline   Reply With Quote
Old 01-01-2007, 08:02 PM   PM User | #7
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
I know that, I want the code for my own web-site !
alMubarmij is offline   Reply With Quote
Old 01-10-2007, 06:06 PM   PM User | #8
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
Thank you all for your support.

but I have another problem now, and I hope to continue your help.

I want now to replace the each digite number (1, 2, 3 ...) by Unicode codes like:
Code:
&#1500 ;
&#1220 ;
&#1200 ;
&#1202 ;
&#1203 ;
..etc.
if I used the previous code it will replace the Unicode code numbers.

do you have any solution for that ?

Last edited by alMubarmij; 01-10-2007 at 06:21 PM..
alMubarmij is offline   Reply With Quote
Old 01-11-2007, 10:39 AM   PM User | #9
alMubarmij
New Coder

 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
alMubarmij is an unknown quantity at this point
More Explaination:

I want something doing like this:
Code:
0 = &#1200 ;
1 = &#1500 ;
2 = &#1220 ;
3 = &#1201 ;
4 = &#1202 ;
5 = &#1203 ;
6 = &#1204 ;
7 = &#1205 ;
8 = &#1206 ;
9 = &#1207 ;
Note:
I put a space before ";" because the forum treat with the code as a HTML, and it will replace it.
alMubarmij is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:22 AM.


Advertisement
Log in to turn off these ads.