Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   String splitting without loosing punctuation marks (http://www.velocityreviews.com/forums/t924945-string-splitting-without-loosing-punctuation-marks.html)

Anat 05-25-2006 07:23 AM

String splitting without loosing punctuation marks
 
Hi,
I need a little help on performing string manipulation:
I want to take a given string, and make certain words hyperlinks.
For example:
"Hello world, this is a wonderful day!"
I'd like the words world & and day to be hyperlinks, therefore
after my manipulation it should be:
"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
Using split method is not good, because splitting with regex each
punctuation mark causes the commas and other punctuation marks to
disappear.
Instead of displaying
"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
I will display
"Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
(note that the comma and exclamation mark are gone).
Any ideas on how I can locate words, replace them but not loose
punctuation marks on the way?
Thanks a lot!!!
Anat.


Lee 05-25-2006 11:24 AM

Re: String splitting without loosing punctuation marks
 
Anat said:
>
>Hi,
>I need a little help on performing string manipulation:
>I want to take a given string, and make certain words hyperlinks.
>For example:
>"Hello world, this is a wonderful day!"
>I'd like the words world & and day to be hyperlinks, therefore
>after my manipulation it should be:
>"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
>Using split method is not good, because splitting with regex each
>punctuation mark causes the commas and other punctuation marks to
>disappear.
>Instead of displaying
>"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
>I will display
>"Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
>(note that the comma and exclamation mark are gone).
>Any ideas on how I can locate words, replace them but not loose
>punctuation marks on the way?


The words are "losing" and "lose", not "loosing" and "loose".
Split on whitespace and then handle punctuation separately.


--


Lasse Reichstein Nielsen 05-25-2006 11:47 AM

Re: String splitting without loosing punctuation marks
 
"Anat" <anat.sever@verint.com> writes:

> "Hello world, this is a wonderful day!"
> I'd like the words world & and day to be hyperlinks, therefore
> after my manipulation it should be:
> "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"


.... and these words were chosen because they are just before
punctuation, I guess.

> Using split method is not good, because splitting with regex each
> punctuation mark causes the commas and other punctuation marks to
> disappear.
> Instead of displaying
> "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
> I will display
> "Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
> (note that the comma and exclamation mark are gone).


If you want to replace parts of a string ... use the replace function:

string.replace(/\b(\w+)([.,!?])/g, "<a href=\"...\">$1<\/a>$2")

> Any ideas on how I can locate words, replace them but not loose
> punctuation marks on the way?


Replace and keep the punctuation mark in the replacement.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


All times are GMT. The time now is 03:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.