Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Closed Thread
Javascript Replace Words
Old 01-20-2009, 09:37 AM Javascript Replace Words
Junior Talker

Posts: 3
Trades: 0
Hello,
I am have a javascript code I do replace some words of content pages.

The code is:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.toolTip {
cursor: help;
font-weight: bold;
}
</style>
</head>
<body>
<div>Bucuresti, I think Sibiu is a Hot Oradea.<br>
Hello and welcome in Oradea to this great sporting event.<br>
<span>Sibiu</span><br>Bucuresti
</div>
<script type="text/javascript">

;(function(){
 var addTips = {
  Oradea : 'Capital of Bihor County in Crisana, Romania',
  Sibiu : 'Capital of Sibiu County, Located some 282 km NW of Bucharest',
  Bucuresti : 'Capital City, Industrial, and Commercial Center of Romania'
 },
 tipWords = [], allTableData = document.getElementsByTagName('td'),
 allTableHeaders = document.getElementsByTagName('div'),
 collections = [allTableData, allTableHeaders],
 carve = function(tnm, cn){
  var text = cn.nodeValue.replace(tipWords, '?!$').split('?!$');
  for(var tip, n = [], i = 0; i < text.length; ++i){
   n.push(document.createTextNode(text[i]));
   if(tnm[i]){
    tip = document.createElement('span');
    tip.className = 'toolTip';
    tip.title = addTips[tnm[i]];
    tip.appendChild(document.createTextNode(tnm[i]));
    n.push(tip);
   }
 }
 for (var i = n.length - 1; i > -1; --i)
  i == n.length - 1? cn.parentNode.replaceChild(n[i], cn) : n[i+1].parentNode.insertBefore(n[i], n[i+1]);
 };
 for (var p in addTips)
  if (addTips.hasOwnProperty(p))
   tipWords.push(p);
 tipWords = new RegExp('(' + tipWords.join(')|(') + ')', 'g');
 for (var k = collections.length - 1; k > -1; --k)
  for (var cn, tnm, i = collections[k].length - 1; i > -1; --i){
   cn = collections[k][i].childNodes;
   for (var j = cn.length - 1; j > -1; --j)
    if(cn[j].nodeType == 3 && (tnm = cn[j].nodeValue.match(tipWords)))
     carve(tnm, cn[j]);
 }
})();

</script>
</body>
</html>
I can help someone to change the script so that you do the following:

1) Words in text Bucuresti and Oradea repeated the script I do replace all the words, I would like to replace a word only once.

2) I'd like to be replace only if the word is exactly and not replace the word if for example found Bucurestilor i like to not replace with Bucuresti.

I would be deeply grateful if you can help me, thank you
alexio is offline
View Public Profile
 
 
Register now for full access!
Old 01-20-2009, 11:42 AM Re: Javascript Replace Words
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
tipWords = new RegExp('(' + tipWords.join(')|(') + ')', 'g');
The "g" means global, which will match all instances

for exact word matching add "\b" (Word boundary matching) to the start and end of the search expression(s)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 01-20-2009, 12:19 PM Re: Javascript Replace Words
Junior Talker

Posts: 3
Trades: 0
Frankly do not understand why should I change and where to put the "\ b" are very beginner in javascript.
I tried to put:
Code:
tipWords = new RegExp('(' + tipWords.join(')|(') + ')', '\b');
I apologize if I did not understand what you wanted to say, I know that it will kidnap of time but I would like to explain mi better if you can
alexio is offline
View Public Profile
 
Old 01-20-2009, 06:22 PM Re: Javascript Replace Words
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
your search word needs to look like "\bword\b" and leave off the last parameter totally

so
Code:
tipWords = new RegExp('(\b' + tipWords.join(\b')|('\b) + '\b)');
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 01-21-2009, 08:18 AM Re: Javascript Replace Words
Junior Talker

Posts: 3
Trades: 0
Your example does not work and and here "(\ b')|(' \ b)" is wrong, at least so I look editor Aptana.

I tried to solve the problem but leaving a space not function properly, I put:
Code:
tipWords = new RegExp(' (' + tipWords.join(')|(') + ') ', 'g');
I bold words well, but not take my word description of how, for some words instead of my description shows "undefined"
alexio is offline
View Public Profile
 
Old 01-21-2009, 08:23 AM Re: Javascript Replace Words
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok I'll actually test something instead of on the fly programming.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Closed Thread     « Reply to Javascript Replace Words
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24973 seconds with 12 queries