By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

How to extract individual word and change them into hyperlinks?

P: 17
Hi,all.
I want to extract all words ( not including punctuations) from a text and convert all individual words into hyperlinks directing a dictionary site. When clicking on any word will go to the dict site. How to write the javascript?
For example:
"This is a text for extraction."
The dictionary site is http://www.dic.com/
The html should be like:
This is a text for extraction.
The embedded links should be : http://www.dic.com/This
http://www.dic.com/is
http://www.dic.com/a
http://www.dic.com/text
...
Thank you very much in advance.
Oct 27 '07 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,390
hi ...

let me give you an idea:

Expand|Select|Wrap|Line Numbers
  1. // we declare an empty string that we use as a new html-block
  2. // later on
  3. var new_html = '';
  4.  
  5. // your text-value that you have from wherever
  6. var text = 'This is a text for extraction.';
  7.  
  8. // now we create a list from the sentence with a regEx
  9. // and leave the punctuations
  10. var words = text.match(/(\w[^ ,.!?]+)/g);
  11.  
  12. // we create the new html block here
  13. for (var i = 0; i < words.length; i++) {
  14.     var word = words[i];
  15.  
  16.     new_html += '<a href="http://www.dic.com/' + word + '">' + word + '</a>';
  17. }
  18.  
  19. // alert the new html block code
  20. alert(new_html);
  21.  
have a look at the comments, now you simply have to add new_html wherever you want ... use innerHTML for that ...

kind regards
Oct 27 '07 #2

P: 17
gits. You are great.

Thank you very much for your kind and detailed answer. Your answer should be awarded 5 stars though I have not tested it.
Oct 27 '07 #3

gits
Expert Mod 5K+
P: 5,390
hi :)

lets have a look whether it works for you ... post back anytime in case have problems with it ...

kind regards
Oct 27 '07 #4

P: 17
Gits:

It partially works.

There is a text including "(" and "--", and"." and "," and "..." and "$"

For example
"RAMADI, Iraq (AP) -- The women received their first paychecks a few weeks ago - about $500 for a month's work as police officers. They paid rent, bought food, wiped out debts. But the seemingly simple transaction has left at least one woman in fear for her life, another threatened with divorce...."

Your script parsed it as:
"RAMADI Iraq AP) The women received their first paychecks few weeks ago about 500 for month's work as police officers They paid rent bought food wiped out debts But the seemingly simple transaction has left at least one woman in fear for her life another threatened with divorce "

Why?
Please help.
Oct 27 '07 #5

gits
Expert Mod 5K+
P: 5,390
hi ...

it was a simple example :) ... have a look at the regEx ... it simply matches all regular characters ... and we leave ' ' and '.' and etc. for sure ... so you may adapt the regEx to your needs ...

kind regrads
Oct 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.