String splitting without loosing punctuation marks

A

Anat

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.
 
L

Lee

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.


--
 
L

Lasse Reichstein Nielsen

Anat said:
"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+)([. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top